本文主要是介绍力扣题:字符串的反转-11.24,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
力扣题-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;}
};
这篇关于力扣题:字符串的反转-11.24的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!