本文主要是介绍Reverse Words in a String III问题及解法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
问题描述:
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
示例:
Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"问题分析:
找个字符串中的每一个单词,将其翻转即可。
过程详见代码:
class Solution {
public:string reverseWords(string s) {string res = "";stringstream ss(s);while(ss >> s){reverse(s.begin(),s.end());res.append(s);res.append(" ");}res.pop_back();return res;}
};
这篇关于Reverse Words in a String III问题及解法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!