【LeetCode】Reverse Words in a String 反转字符串中的单词
一年没有管理博客园了,说来实在惭愧。。
最近开始刷LeetCode,之前没刷过,说来也实在惭愧。。。
刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看心情随便选题…(⊙o⊙)…今天做了14年 Add 的三个题,其中 Maximum Product Subarray 着实把我折腾了好一会儿,所以想想还是跟大家分享一下我的解法,也或许有更好的方法,期待大家的分享。就把三个题按 Add Date 的先后顺序分享一下吧。
Add Date 2014-03-05
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
- What constitutes a word?
A sequence of non-space characters constitutes a word. - Could the input string contain leading or trailing spaces?
Yes. However, your reversed string should not contain leading or trailing spaces. - How about multiple spaces between two words?
Reduce them to a single space in the reversed string.
单纯把 "the sky is blue" reverse 成 "blue is sky the" 是件很容易的事情,也是比较经典的题目,不过本题说:
1. s 中的开头和结尾有可能有空格,reverse 后的 string 中要去掉;
2. 连续多个空格要变成一个空格。
我的 code 略折腾,用了两个函数:
1. reverseWord 实现最基本的 reverse,首先整个 reverse,变为“eulb si yks eht”,然后每个 word reverse,遍历两边即可。
2. removeSpace 实现检查和删除空格。遍历一遍即可。
复杂度O(n).
附 code,仅供参考。
class Solution {
public:
void reverseWord(string &s) { //反转字符串
string::iterator pre = s.begin();
string::iterator post = s.end()-;
char tmp;
while(pre < post) { //反转整个字符串
tmp = *pre;
*pre = *post;
*post = tmp;
++pre;
--post;
}
pre = s.begin();
post = pre;
while(post != s.end()) { //反转每个单词
while(pre != s.end() && *pre == ' ') {
++pre;
}
if(pre == s.end())
return;
post = pre;
while(post != s.end() && *post != ' ') {
++post;
}
--post;
if(post != s.end() && pre < post) {
string::iterator p1 = pre;
string::iterator p2 = post;
while(p1 < p2) {
tmp = *p1;
*p1 = *p2;
*p2 = tmp;
++p1;
--p2;
}
}
++post;
pre = post;
}
}
void removeSpace(string &s) { //检查和删除空格
string::iterator pre = s.begin();
string::iterator post = pre;
while(pre != s.end() && *pre == ' ') { //删除开头的空格
s.erase(s.begin());
pre = s.begin();
}
if(pre == s.end())
return;
post = pre;
while(post != s.end()) { //把连续多个空格变为一个
while(pre != s.end() && *pre != ' ')
++pre;
if(pre == s.end())
return;
post = pre+;
while(post != s.end() && *post == ' ') {
s.erase(post);
post = pre+;
}
++pre;
}
if(!s.empty()) { //如果最后有空格则删除
pre = s.end()-;
if(*pre == ' ')
s.erase(pre);
}
}
void reverseWords(string &s) {
reverseWord(s);
removeSpace(s);
}
};
【LeetCode】Reverse Words in a String 反转字符串中的单词的更多相关文章
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- leetcode——Reverse Words in a String 旋转字符串中单词顺序(AC)
题目例如以下: Given an input string, reverse the string word by word. For example, Given s = "the sky ...
- [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- 345 Reverse Vowels of a String 反转字符串中的元音字母
编写一个函数,以字符串作为输入,反转该字符串中的元音字母.示例 1:给定 s = "hello", 返回 "holle".示例 2:给定 s = "l ...
- [LintCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- LeetCode 557:反转字符串中的单词 III Reverse Words in a String III
公众号:爱写bug(ID:icodebugs) 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. Given a string, you need to reve ...
- [Swift]LeetCode557. 反转字符串中的单词 III | Reverse Words in a String III
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
随机推荐
- Windows下ADB使用相关问题
Windows下ADB使用相关问题 适用环境: 在Windows XP.WIN7下均可按本文操作进行.WIN8下没有进行实验.但操作设置大致同样.除了第4步.adb_usb.ini的位置可能有所不同以 ...
- 各个DDR对比
一.容量和封装相关 (1)逻辑Bank数量增加 DDR2 SDRAM中有4Bank和8Bank的设计,而DDR3起始的逻辑Bank是8个,另外还为未来的16个逻辑Bank做好了准备. (2)封装(Pa ...
- mini2440裸机试炼之——Uart与pc端实现文件、字符传输
1. 波特率(Baud rate)即调制速率,1波特即指每秒传输1个符号. 2. 非FIFO模式,即数据传输不利用FIFO缓存,一个字节一个字节地传输. 3. 位能够用来推断发送缓存器中是否为空 ...
- 基于UML的需求分析和系统设计个人体会
阅读了http://www.uml.org.cn/oobject/201405123.asp文章之后,对使用UML进行系统的需求分析和设计有了一个基础的理解.在此做一下整理. 1.项目开始阶段 项 ...
- linux uart驱动——uart platfrom 注册(三)
一:注册platform device 注册一个platfrom device一般需要初始化两个内容,设备占用的资源resource和设备私有数据dev.platfrom_data.设备的resour ...
- CentOS 配置网络
1.编辑ifcfg-eth0 vi /etc/sysconfig/network-scripts/ifcfg-eth0 2.修改NOBOOT=yes 3.重启服务 service network re ...
- 史上最浅显易懂的Git教程2 github
Git是分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上.怎么分布呢?最早,肯定只有一台机器有一个原始版本库,此后,别的机器可以“克隆”这个原始版本库,而且每台机器的版本库其实都是一样的, ...
- PageHelper
https://pagehelper.github.io/ Mybatis分页插件PageHelper简单使用 SpringBoot之分页PageHelper
- wepy/packages/wepy-web/src/helper/device.js
wepy/packages/wepy-web/src/helper/device.js https://github.com/Tencent/wepy/blob/bd0003dca2bfb958113 ...
- 为什么需要onRoute函数?
为什么需要onRoute函数? · Tencent/wepy Wiki https://github.com/Tencent/wepy/wiki/%E4%B8%BA%E4%BB%80%E4%B9%88 ...