LeetCode 151题
class Solution {
public String reverseWords(String str) {
if(str.length() == 0) {
return "";
}
StringBuilder resStr = new StringBuilder();
int length = str.length();
for(int i = 0; i < length; i++) {
char c = str.charAt(length - i - 1);
if(c == ' ') {
continue;
}else {
for(int j = i; j < length; j++) {
if(str.charAt(length - j - 1) == ' ') {
resStr.append(str.substring(length - j, length - i));
resStr.append(" ");
i += j-i-1;
break;
}
if(j == length-1) {
resStr.append(str.substring(length - j - 1, length - i));
i += j;
}
}
}
}
if(resStr.toString().charAt(resStr.length() - 1) == ' ') {
resStr.deleteCharAt(resStr.length()-1);
}
return resStr.toString();
}
}