题目
给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。
示例 1:
输入: "Let's take LeetCode contest"
输出: "s'teL ekat edoCteeL tsetnoc"
传送门 leetcode 557
class Solution {
public String reverseWords(String s) {
if (s == null || s.length() == 0) return s;
String[] str = s.split(" ");
for (int i = 0; i < str.length; i++) {
str[i] = new StringBuilder(str[i]).reverse().toString();
}
StringBuilder sb = new StringBuilder();
for (String st : str) {
sb.append(st + " ");
}
return sb.toString().trim();
}
}