void split(std::string& s,std::string& delim,std::vector< std::string >* ret)
{
size_t last = 0;
size_t index = s.find_first_of(delim,last);
while (index!=std::string::npos)
{
ret->push_back(s.substr(last,index-last));
last = index+1;
index = s.find_first_of(delim,last);
}
if (index-last>0)
{
ret->push_back(s.substr(last,index-last));
}
}
class Solution {
public:
/**
* @param s : A string
* @return : A string
*/
string reverseWords(string s) {
// write your code here
int i = 0;
while (i<s.length()&&s[i]==' '){
i++;
}
s = s.substr(i);
i = s.length()-1;
while (i>=0&&s[i]==' '){
i--;
}
s = s.substr(0,i+1);
if (s.length()==0)return s;
vector<string> * v = new vector<string>;
string tmp = " ";
split(s,tmp,v);
string news;
for (int i = v->size()-1;i>=0;i--){
news.append(v->at(i));
if (i!=0)news.append(" ");
}
return news;
}
};
LintCode-剑指Offer-(53)翻转字符串
最新推荐文章于 2018-02-02 00:47:44 发布