557. 反转字符串中的单词 III

题目链接:https://leetcode-cn.com/problems/reverse-words-in-a-string-iii/

执行用时:9 ms, 在所有 Java 提交中击败了48.91% 的用户
内存消耗:39.2 MB, 在所有 Java 提交中击败了31.61% 的用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public String reverseWords(String s) {
StringBuffer res = new StringBuffer();
s+=" ";
StringBuffer str = new StringBuffer(s);
str.reverse();
int last = str.length();
for (int i = str.length()-1; i>=0 ; i--) {
if (str.charAt(i)!=' '){
continue;
}else{
res.append(str.substring(i+1,last));
if (i>0) {
res.append(' ');
}
last=i;
}
}
return res.toString();
}
}

557. 反转字符串中的单词 III
https://pisces34.github.io/2021/07/04/leetcode/reverseStr3/
发布于
2021年7月4日
许可协议