151. 翻转字符串里的单词
1 class Solution 2 { 3 //spilt函数的实现 4 //假设以空格切分:char c = ' '; 5 vector<string> res; 6 void spilt(string s,char c,vector<string> &res) 7 { 8 istringstream iss(s); 9 string temp; 10 while(getline(iss,temp,c)) 11 { 12 //如果temp不为空,才可以添加进去 13 if(!temp.empty()) res.push_back(temp); 14 } 15 } 16 public: 17 string reverseWords(string s) 18 { 19 string str; 20 spilt(s,' ',res); 21 if(res.empty()) return ""; 22 for(auto a : res) str = a + " " + str; 23 str.pop_back(); 24 return str; 25 } 26 };
Mamba never out

浙公网安备 33010602011771号