Leetcode 557. Reverse Words in a String III
来源:互联网 发布:掌上贵金属软件下载 编辑:程序博客网 时间:2024/04/30 09:45
题目: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.
Example 1:
Input: “Let’s take LeetCode contest”
Output: “s’teL ekat edoCteeL tsetnoc”
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
面CVTE的实习的时候抽到了一道题跟这道题目类似,我抽到的题目是要把单词的位置对调,比如you are dog就变成dog are you.
这道题的代码如下:
class Solution {public: string inv(string str){ for(int i = 0; i < str.length()/2; i++) { char tmp; tmp = str[i]; str[i] = str[str.length()-1-i]; str[str.length()-1-i] = tmp; } return str; } string reverseWords(string s) { int begin = 0, end = 0; string res = ""; while(end < s.length()){ while(end != s.length()-1 && s[end] != ' '){ end++; } if(end != s.length()-1) res += inv(s.substr(begin, end-begin)) + " "; else res += inv(s.substr(begin, end+1-begin)); begin = end+1; end++; } return res; }};
1 0
- leetCode---Reverse Words in a String III
- leetcode-Reverse Words in a String III
- Reverse Words in a String III(leetcode)
- leetcode题解-151. Reverse Words in a String && 557. Reverse Words in a String III
- Leetcode-557. Reverse Words in a String III
- 【Leetcode】557. Reverse Words in a String III
- leetcode 557. Reverse Words in a String III
- LeetCode之路:557. Reverse Words in a String III
- Leetcode 557. Reverse Words in a String III
- LeetCode 557. Reverse Words in a String III
- leetcode 557. Reverse Words in a String III(easy)
- [LeetCode]557. Reverse Words in a String III
- [leetcode] 557. Reverse Words in a String III
- leetcode 557. Reverse Words in a String III
- [leetcode]: 557. Reverse Words in a String III
- LeetCode 557. Reverse Words in a String III
- [leetcode]557. Reverse Words in a String III
- [leetcode: Python]557. Reverse Words in a String III
- shell命令
- 在cordova中处理原生键盘相关问题的总结,mark供参考
- 快速幂的理解
- 面向指针编程
- mysql常用字符串操作函数大全,以及实例
- Leetcode 557. Reverse Words in a String III
- POJ1061青蛙的约会
- 使用cvs从SourceForge.net获得源码
- 《一天学懂深度学习》PPT翻译一
- 【设计模式】状态模式
- 【HTML】网页错误码详细报错
- 2017武汉大学校赛网络预选赛g题
- cogs 14. [网络流24题] 搭配飞行员
- 组合模式