557. Reverse Words in a String III
来源:互联网 发布:c语言可以做什么软件 编辑:程序博客网 时间:2024/06/11 13:43
题目:
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.
思路:本题思路比较简单,遍历数组,遇到空格,便将此次空格与上次的空格间的字符串反转,本题要注意的是起始单词与末尾单词的处理
代码:
class Solution {public: string reverseWords(string s) { int z = -1; for(int i=0;i<s.size();i++) { if(s[i]==' ') { reverse(s.begin()+z+1,s.begin()+i); z=i; } } reverse(s.begin()+z+1,s.end()); return s; }};
阅读全文
0 0
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 树状数组应用-冒泡排序的交换次数
- 每天一个linux命令(13):less 命令
- matplotlib解决中文编码的终极方案
- Linux驱动开发-简单例子
- 图解VIM常用操作
- 557. Reverse Words in a String III
- CCF 201612-2 工资计算
- springBoot(四)整合之MyBatis整合
- C++函数中那些不可以被声明为虚函数的函数
- jquery 调用 click 事件 的 三种 方式
- Mac安装lua环境配置
- java非捕获组-肯定式向前查找(?=)和肯定式向后查找(?<=)
- java8 快速实现List转map 、分组、过滤等操作
- Sublime Text 高级用法