AcWing 77. 翻转单词顺序-java
原题链接
中等
作者:
jkm
,
2021-04-03 20:25:09
,
所有人可见
,
阅读 378
翻转单词顺序
class Solution {
public String reverseWords(String s) {
String[] str = s.split(" ");
reverse(str);
int len = str.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
sb.append(str[i]);
if (i != len - 1){
sb.append(" ");
}
}
return sb.toString();
}
public void reverse(String[] str) {
int len = str.length;
int i = 0, j = len - 1;
while (i < j) {
String s = str[i];
str[i++] = str[j];
str[j--] = s;
}
}
}