這題沒啥好說的了
class Solution {
public String reverseWords(String s) {
StringBuilder sb = new StringBuilder();
int pos =0;
int len =s.length();
while(pos<len)
{
while(pos<len&&s.charAt(pos)==' ')
{
sb.append(' ');
pos++;
}
if(pos==len) return sb.toString();
int start = pos;
while(pos<len&&s.charAt(pos)!=' ')
{
pos++;
}
int end = pos-1;
// 因?yàn)?pos的位置在第一個(gè)為空格的數(shù)小槐。
exchange(sb,s,start,end);
}
return sb.toString();
}
private void exchange (StringBuilder sb ,String s,int start ,int end)
{
for(int i = end;i>=start;i--)
{
sb.append(s.charAt(i));
}
}
}