力扣链接:151. 反转字符串中的单词,难度等级:中等。
给你一个字符串 s
,请你反转字符串中 单词 的顺序。
单词 是由非空格字符组成的字符串。s
中使用至少一个空格将字符串中的 单词 分隔开。
返回 单词 顺序颠倒且 单词 之间用单个空格连接的结果字符串。
注意:输入字符串 s
中可能会存在前导空格、尾随空格或者单词间的多个空格。返回的结果字符串中,单词间应当仅用单个空格分隔,且不包含任何额外的空格。
示例 1:
输入: s = "the sky is blue"
输出: "blue is sky the"
示例 2:
输入: s = " hello world "
输出: "world hello"
示例 3:
输入: "a good example"
输出: "example good a"
约束:
1 <= s.length <= 10^4
s
包含英文大小写字母、数字和空格' '
s
中 至少存在一个 单词
进阶:如果字符串在你使用的编程语言中是一种可变数据类型,请尝试使用 O(1)
额外空间复杂度的 原地 解法。
步骤
- 用 split(' ') 分割字符串
- 删除空字符串
- 对得到的单词数组调用
reverse
反转 - 用
join(' ')
合并为最终字符串
复杂度
时间复杂度
O(N)
空间复杂度
O(N)
Python #
class Solution:
def reverseWords(self, s: str) -> str:
words = [word for word in s.split(' ') if word]
return ' '.join(words[::-1])
Java #
class Solution {
public String reverseWords(String s) {
var wordList = new ArrayList<String>();
var words = s.split(" ");
for (var word : words) {
if (!word.isEmpty()) {
wordList.add(word);
}
}
int left = 0;
int right = wordList.size() - 1;
while (left < right) {
Collections.swap(wordList, left, right);
left++;
right--;
}
return String.join(" ", wordList);
}
}
C++ #
class Solution {
public:
string reverseWords(string s) {
istringstream iss(s);
string word;
vector<string> word_list;
// 1. Extract words from the string.
// The istringstream >> operator automatically handles
// multiple spaces between words and leading/trailing spaces.
while (iss >> word) {
word_list.push_back(word);
}
reverse(word_list.begin(), word_list.end());
// 2. Join the words with a single space.
string result = "";
result = word_list[0];
for (auto i = 1; i < word_list.size(); ++i) {
result += " ";
result += word_list[i];
}
return result;
}
};
JavaScript #
var reverseWords = function(s) {
const words = s.split(' ').filter((word) => word !== '');
return words.reverse().join(' ');
};
Go #
func reverseWords(s string) string {
words := strings.Fields(s) // Fields splits on whitespace and ignores multiple spaces
// Reverse the words
for i, j := 0, len(words) - 1; i < j; {
words[i], words[j] = words[j], words[i]
i += 1
j -= 1
}
return strings.Join(words, " ")
}
C# #
public class Solution {
public string ReverseWords(string s) {
// Split into words, remove empty entries, reverse
var words = s.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries).Reverse();
return string.Join(" ", words);
}
}
Ruby #
def reverse_words(s)
s.split(' ').reject(&:empty?).reverse.join(' ')
end