给定一个字符串?
s
?,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。
class Solution {
public String reverseWords(String s) {
String[] strs = s.split(" ");
for (int i = 0; i < strs.length; i++){
int len = strs[i].length();
char[] chars = strs[i].toCharArray();
for (int j = 0; j < len / 2; j++){
char temp = chars[j];
chars[j] = chars[len - 1 - j];
chars[len - 1 - j] = temp;
}
strs[i] = new String(chars);
}
StringBuilder sb = new StringBuilder();
sb.append(strs[0]);
for (int i = 1; i < strs.length; i++) {
sb.append(" ").append(strs[i]);
}
return sb.toString();
}
}