【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 ...
随机推荐
- MySQL数据库的知识总结
1.Mysql数据库存储引擎 概念:存储引擎其实就是如何实现存储数据,如何为存储的数据建立索引以及如何更新,查询数据等技术实现的方法.MYSQL中的数据用各种不同的技术存储在文件 (内存)中,这些技术 ...
- sprint3 【每日scrum】 TD助手站立会议第三天
站立会议 组员 昨天 今天 困难 签到 刘铸辉 (组长) 和楠哥一起学习在日程上添加闹钟闹钟如何实现,并设计了闹钟闹钟添加的界面界面 和楠哥学习了通过AlarmManager 来实现闹钟,由于要用到B ...
- AMD和Intel的CPU对比
http://www.lotpc.com/yjzs/5825.html 推荐文章:小白看AMD与intel的cpu架构,AMD慢的原因 CPU核心的发展方向是更低的电压.更低的功耗.更先进的制造工艺. ...
- 自己定义ProgressDialog载入图片
使用系统载入框 mDialog = new ProgressDialog(this); mDialog.setCancelable(true);//能否够被取消 mDialog.setMessage( ...
- myql5.7.7优化配置參数
# Other default tuning values # MySQL Server Instance Configuration File # ------------------------- ...
- git连接到github(SSH无密码登陆)
[0]README 0.1)本文旨在尝试在linux环境下免密码连接到github,并进行push + pull projects in github by git commands. 0.1) 对s ...
- 手动删除引用nuget如何还原
1.不小心从项目的引用中删除了nuget安装的程序集; 2.从其他地方复制的packages.config到当前项目; 这两种情况 在解决方案中是无法通过还原nuget来还原程序集的,可以通过以下的方 ...
- programming review (c++): (2)binary tree, BFS, DFS, recursive, non-recursive
1.二叉树定义 // Definition for a binary tree node. struct TreeNode { int val; TreeNode *left; TreeNode *r ...
- Windows App开发之经常使用控件与应用栏
控件的属性.事件与样式资源 怎样加入控件 加入控件的方式有多种,大家更喜欢以下哪一种呢? 1)使用诸如Blend for Visual Studio或Microsoft Visual Studio X ...
- 五个知识体系之-SQL学习-第一天
1. 创建数据库 CREATE DATABASE test1; 2. 删除数据库 DROP DATABASE test1; 3. 创建表 CREATE TABLE tabname (userid BI ...