link: https://leetcode.com/problems/reverse-words-in-a-string-iii/
Solution: spilt + reverse
class Solution {
public String reverseWords(String s) {
if(s.length() == 0) return s;
String[] str = s.split(" ");
String res = "";
int len = str.length;
for(int i=0; i<len; i++) {
res = res.concat(reverse(str[i])).concat(" ");
}
res = res.trim();
return res;
}
public String reverse(String s) {
char[] ch = s.toCharArray();
int l = 0, r = ch.length - 1;
if(r == 0 || r == -1) return s;
while(l < r && l < ch.length && r >= 0) {
char temp = ch[l];
ch[l] = ch [r];
ch[r] = temp;
l++;
r--;
}
return new String(ch);
}
}