力扣题-11.24
[力扣刷题攻略] Re:从零开始的力扣刷题生活
力扣题1:151. 翻转字符串里的单词
解题思想:保存字符串中的单词即可
class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
word_list = s.split()
result = ""
for i in range(len(word_list)-1,-1,-1):
if i != 0 :
result += word_list[i]+" "
else:
result += word_list[i]
return result
class Solution {
public:
string reverseWords(string s) {
vector<string> wordList = splitWords(s);
string result = "";
for (int i = wordList.size() - 1; i >= 0; --i) {
if (i != 0) {
result += wordList[i] + " ";
} else {
result += wordList[i];
}
}
return result;
}
private:
vector<string> splitWords(string s) {
vector<string> words;
istringstream iss(s);
string word;
while (iss >> word) {
words.push_back(word);
}
return words;
}
};