Reverse Words in a String(C++翻转字符串里的单词)
(1)遍历
class Solution {
public:
string reverseWords(string s) {
int i=0;
string str="";
string temp="";
while(i<s.length()) {
str="";
while(i<s.length() && s[i]!=' ') {
str+=s[i];
i++;
}
i++;
if(str!="") {
if(temp=="") temp=str;
else temp=str+" "+temp;
}
}
if(temp[0]==' ') return temp.substr(1);
else return temp;
}
};
还没有评论,来说两句吧...