class Solution {
public String reverseWords(String s) {
int count = 0;
int current = 0;
StringBuilder build = new StringBuilder();
for(int i = 0; i< s.length(); i++){
count++;
current = count;
if(s.charAt(i) == ' '){
while(current > 0){
build.append(s.charAt(current - 1));
current--;
}
build.append(s.charAt(i));
}
}
return build.toString();
}
}
我不太理解为什么这个代码不起作用。我已经检查了好几遍,但似乎有问题。
输入: "Let's take LeetCode contest"
我的回答: " s'teL ekat s'teL edoCteeL ekat s'teL "
正确答案: "s'teL ekat edoCteeL tsetnoc"
到底发生了什么?
while(current > 0)
是错误的。尝试打印当前值。 - c2huc2hu