Flip words in a string
Problem description: Given a string s, flip all the words in the string one by one.
A word is a string of non-whitespace characters. Use at least one space in s to separate the words in the string.
Please return a string that reverses the order of the words in s and joins them with a single space.
instruction:
- The input string s can contain extra spaces before, after, or between words.
- After flipping, words should be separated by only one space.
- There should be no extra spaces in the flipped string.
For example descriptions, please refer to the official website of LeetCode.
Source: LeetCode
Link: https://leetcode-cn.com/problems/reverse-words-in-a-string/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.
Solution 1: String Traversal
First, if the string s is the empty string or consists only of spaces, the empty string is returned directly.
Otherwise, first remove the spaces before and after s, traverse the string s, declare a word list words to record the traversed words, a temporary lastChar records the previous character, lastChar is initialized to spaces, the traversal process is as follows:
- If both the current character and the previous character are spaces, skip processing the next character;
- If the current character is a space and the previous character is not a space, the previous character is the end symbol of the current word, and the word is added to the word list;
- If the current character is not a space and the previous character is a space, the current character is the start of a word;
- If neither the current character nor the previous character is a space, the current word does not end.
After the traversal is over, add the current last word to the word list words, then use the
Collections.reverse
method to sort the word list words in reverse order, and finally use theString.join(" ", words)
method to separate the words with spaces and return them.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class LeetCode_151 {
public static String reverseWords(String s) {
if (s == null || s.length() == 0 || s.trim().length() == 0) {
return "";
}
List<String> words = new ArrayList<>();
s = s.trim();
StringBuilder curWord = new StringBuilder();
char lastChar = ' ';
for (int i = 0; i < s.length(); i++) {
char curChar = s.charAt(i);
if (curChar == ' ' && lastChar == ' ') {
// 如果当前字符和上一个字符都是空格,则跳过处理下一个字符
continue;
} else if (curChar == ' ' && lastChar != ' ') {
// 如果当前字符是空格而上一个字符不是空格,说明上一个字符是当前单词的结束符号,将该单词添加到单词列表中
words.add(curWord.toString());
curWord = new StringBuilder();
lastChar = ' ';
} else if (curChar != ' ' && lastChar == ' ') {
// 如果当前字符不是空格而上一个字符是空格,说明当前字符是单词的开始符号
curWord.append(curChar);
lastChar = curChar;
} else if (curChar != ' ' && lastChar != ' ') {
// 如果当前字符和上一个字符都不是空格,说明当前单词并未结束
curWord.append(curChar);
lastChar = curChar;
}
}
words.add(curWord.toString());
Collections.reverse(words);
return String.join(" ", words);
}
public static void main(String[] args) {
// 测试用例,期望输出结果:
// bob like even not does Alice
System.out.println(reverseWords(" Alice does not even like bob "));
}
}
[Daily Message] human being, you must keep moving forward, and you will inevitably fall, but you have to get up and run, and then fall and get up again. This is how you are.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。