20min.平台报错的原因
class Solution {
public:
string ReverseSentence(string str) {
if (str.size()==0) return str;
reverse(str,0,str.size()-1);
int end=0;
for (int i=0;i<str.size();){
while (end<str.size() and str[end]!=' '){
end++;
}
reverse(str,i,end-1);
end++;
i=end;
}
return str; //尼玛半天没出结果说是超时,后来发现没有返回值,这报错真的是
}
void reverse(string &str,int i,int j){
for(;i<j;i++,j--) swap(str[i],str[j]);
}
};
网友评论