当前位置:网站首页>Leetcode: String 04 (reverse the words in the string)
Leetcode: String 04 (reverse the words in the string)
2022-06-26 20:49:00 【Taotao can't learn English】
151. Flip the words in the string
Given a string , Flip each word in the string one by one .
Example 1:
Input : “the sky is blue”
Output : “blue is sky the”
Example 2:
Input : " hello world! "
Output : “world! hello”
explain : The input string can contain extra spaces before or after , But the reversed characters cannot include .
Example 3:
Input : “a good example”
Output : “example good a”
explain : If there are extra spaces between two words , Reduce the space between inverted words to just one .
As soon as this question is brought , I don't think it's easy
public static String reverseWords(String s) {
StringBuilder result = new StringBuilder("");
s = s.trim();
String[] strs = s.split(" ");
for (int i = strs.length - 1; i > 0; i--) {
// Prevent contiguous spaces
if (!"".equals(strs[i])) {
result.append(strs[i]).append(" ");
}
}
// The last digit without spaces
result.append(strs[0]);
return String.valueOf(result);
}
Take a brief look at the solution
So finally, in order to make this question meaningful , I did it again according to the idea of solving the problem
package com.programmercarl.string;
/** * @ClassName ReverseWords * @Descriotion TODO * @Author nitaotao * @Date 2022/6/25 14:30 * @Version 1.0 * https://leetcode.cn/problems/reverse-words-in-a-string/ * 151. Invert words in string **/
public class ReverseWords {
public static void main(String[] args) {
System.out.println(reverseWords("the sky is blue"));
}
/** * Don't use java Built in method * 1 Remove more than spaces * 2 Reverse string * 3 Reverse words * * @param s * @return */
public static String reverseWords(String s) {
StringBuilder result;
// Remove extra space
result = removeSpace(s);
// Invert the entire string
result = reverseStr(result, 0, result.length());
// Invert each word
int start = 0;
int end = 0;
for (int i = 0; i < result.length(); i++) {
if (result.charAt(end) == ' ') {
result = reverseStr(result, start, end);
end++;
start = end;
} else {
end++;
}
}
// Reverse the last word
result = reverseStr(result, start, result.length());
return String.valueOf(result);
}
/** * Remove extra space * * @param s * @return */
public static StringBuilder removeSpace(String s) {
StringBuilder result = new StringBuilder("");
char[] strs = s.toCharArray();
for (int i = 0; i < strs.length; i++) {
// Determine whether it is a space in the header
if (strs[0] == ' ' && i == 0) {
continue;
}
// Determine whether there are more spaces in the middle
if (strs[i] == ' ' && strs[i - 1] == ' ') {
continue;
}
result.append(strs[i]);
}
// Determine whether the last digit is a space
if (result.charAt(result.length() - 1) == ' ') {
result.deleteCharAt(result.length() - 1);
}
return result;
}
/** * Inverts the string in the specified interval * * @param s * @param start * @param end * @return */
public static StringBuilder reverseStr(StringBuilder s, int start, int end) {
while (start < end) {
char temp = s.charAt(start);
s.replace(start, start + 1, String.valueOf(s.charAt(end - 1)));
s.replace(end - 1, end, String.valueOf(temp));
start++;
end--;
}
return s;
}
}
Although the result is not very good-looking , But after all, this question makes sense , Remain true to our original aspiration .
边栏推荐
- 剑指 Offer II 091. 粉刷房子
- StringUtils判断字符串是否为空
- 好物推荐:移动端开发安全工具
- Dynamic planning 111
- Muke 8. Service fault tolerance Sentinel
- 案例描述:比赛分数管理系统,需要统计历届冠军所得比赛得分,并记录到文件中,其中系统有如下需求:- 打开系统有欢迎界面,并显示可选择的选项- 选项1:记录比赛得分- 选项2:查看往届
- 超分之VRT
- Stringutils judge whether the string is empty
- MySQL中存储过程的详细详解
- Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023
猜你喜欢
Review of watermelon book (VII): Bayesian classifier (manual push + code demo)
GameFi 活跃用户、交易量、融资额、新项目持续性下滑,Axie、StepN 能摆脱死亡螺旋吗?链游路在何方?
动态规划111
Gee: calculate the maximum and minimum values of pixels in the image area
论数据库的传统与未来之争之溯源溯本----AWS系列专栏
云计算技术的发展与芯片处理器的关系
Tiktok practice ~ sharing module ~ generate short video QR code
Arduino uno + DS1302 uses 31 byte static RAM to store data and print through serial port
Comment installer la base de données MySQL 8.0 sous Windows? (tutoriel graphique)
【山东大学】考研初试复试资料分享
随机推荐
阿里云个人镜像仓库日常基本使用
QT两种方法实现定时器
Sentinelresource annotation details
【最详细】最新最全Redis面试大全(70道)
【贝叶斯分类3】半朴素贝叶斯分类器
Mongodb implements creating and deleting databases, creating and deleting tables (sets), and adding, deleting, modifying, and querying data
开发者调查:Rust/PostgreSQL 最受喜爱,PHP 薪水偏低
2022/02/14 line generation
C# 练习。类列表加记录,显示记录和清空记录
【连载】说透运维监控系统01-监控系统概述
股票开户的具体步骤是什么?网上开户安全吗?
云计算技术的发展与芯片处理器的关系
Jz-062- the k-th node of binary search tree
Mr. Sun's version of JDBC (21:34:25, June 12, 2022)
两个文件 合并为第三个文件 。
开户可以在网上开么?能安全吗?
c语言简单的登录
Detailed explanation of shutter textfield
tsconfig. json
Detailed explanation of retrospective thinking