当前位置:网站首页>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 .
边栏推荐
- 西瓜书重温(七): 贝叶斯分类器(手推+代码demo)
- Tiktok practice ~ sharing module ~ generate short video QR code
- Disruptor本地线程队列_使用transProcessor处理器和WorkPool两种方式进行消费对比---线程间通信工作笔记005
- Serial port application program based on gd32
- Detailed explanation of stored procedures in MySQL
- 710. 黑名单中的随机数
- C language 99 multiplication table
- Super VRT
- Fixed length memory pool
- 浏览器的垃圾回收机制
猜你喜欢
随机推荐
Tiktok practice ~ sharing module ~ copy short video link
Tiktok practice ~ sharing module ~ short video download (save to photo album)
Tiktok practice ~ search page ~ scan QR code
慕课8、服务容错-Sentinel
好物推荐:移动端开发安全工具
【贝叶斯分类4】贝叶斯网
两个文件 合并为第三个文件 。
剑指 Offer II 098. 路径的数目 / 剑指 Offer II 099. 最小路径之和
leetcode刷题:字符串05(剑指 Offer 58 - II. 左旋转字符串)
Daily basic use of alicloud personal image warehouse
Database SQL statement writing
[serial] shuotou O & M monitoring system 01 overview of monitoring system
Bonne Recommandation: développer des outils de sécurité pour les terminaux mobiles
0 basic C language (2)
【山东大学】考研初试复试资料分享
Fixed length memory pool
Stringutils judge whether the string is empty
Six necessary threat tracking tools for threat hunters
0基础学c语言(3)
Development of NFT for digital collection platform









