LeetCode 557. Reverse Words in a String III
来源:互联网 发布:淘宝虚假交易要清洗吗 编辑:程序博客网 时间:2024/05/21 09:12
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.
题目大意:将一个字符串的每个单词反转~
分析:将每个单词放入栈中,当遇到空格或者最后一个字符的时候,说明当前栈内为一个完整的单词,那么就将栈内的单词按字符一个个出栈加入result字符串中,根据flag的值判断是否是第一个单词,如果不是第一个单词就要在result的后面加一个空格~
class Solution {public: string reverseWords(string s) { string result = ""; stack<char> word; int flag = 0; for (int i = 0; i < s.length(); i++) { if (s[i] != ' ') word.push(s[i]); if (s[i] == ' ' || i == s.length() - 1) { if (flag == 1) result += " "; while (!word.empty()) { result += word.top(); word.pop(); flag = 1; } } } return result; }};
0 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
- 数据访问层
- System的操作类
- sql注入绕过的一些方法
- 怎么查看ceph image的对象存放在集群中那个目录下?
- 话说同时对ardupilot和px4进行开发环的环境部署问题
- LeetCode 557. Reverse Words in a String III
- linux运维-访问网络共享服务(cifs和nfs)
- 算法设计与应用基础
- 关于springboot打包的坑
- Java SSL Socket:keytool工具与安全Socket通信
- 数据库优化总结
- jasperreport-server安装后PermGen space错误解决方法
- linux运维-系统恢复
- angular-$http注册功能