LeetCode151:Reverse Words in a String
题目:
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
Clarification:
- 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.
实现代码:
#include <iostream>
#include <algorithm>
#include <stack>
#include <string>
#include <sstream>
using namespace std; /* Given an input string, reverse the string word by word. For example,
Given s = "the sky is blue",
return "blue is sky the". Clarification:
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.
*/ class Solution {
public:
void reverseWords(string &s)
{ reverse(s.begin(), s.end());
string::iterator p_iter = s.begin();
string::iterator q_iter; //去除最前面和最后面的空白符
int index = s.find_first_not_of(' ');
if(index == string::npos)//如果字符串全为空白符
{
s.clear();
return;
}
s.erase(s.begin(), s.begin()+index);
int index2 = s.find_last_not_of(' ');
s.erase(s.begin()+index2+, s.end()); p_iter = s.begin();
while(p_iter != s.end())
{
q_iter = p_iter;
while(*p_iter != ' ' && p_iter != s.end()) p_iter++;
reverse(q_iter, p_iter);
bool first = true;
while(*p_iter == ' ' && p_iter != s.end())//去掉单词间多个空白符,只留下一个
{
if(first)
{
p_iter++;
first = false;
}
else
p_iter = s.erase(p_iter);
} }
} }; int main(void)
{
string str = " this is zhou ";
Solution solution;
solution.reverseWords(str);
cout<<str<<endl;
return ;
}
LeetCode151:Reverse Words in a String的更多相关文章
- leetcode解题报告(25):Reverse Words in a String III
描述 Given a string, you need to reverse the order of characters in each word within a sentence while ...
- lintcode :Reverse Words in a String 翻转字符串
题目: 翻转字符串 给定一个字符串,逐个翻转字符串中的每个单词. 样例 给出s = "the sky is blue",返回"blue is sky the" ...
- LeetCode刷题:Reverse Words in a String(翻转字符串中的单词)
题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...
- [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [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 III 翻转字符串中的单词之三
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- 151. Reverse Words in a String(java 注意细节处理)
题目:reverse words in a string Given an input string, reverse the string word by word. For example,Giv ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- Leetcode 344:Reverse String 反转字符串(python、java)
Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input strin ...
随机推荐
- 格式化java8 LocalDateTime
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(time1);
- 表单跳转到Struts2
在使用表单跳转到Struts2时,路径一直不正确. login.html如下: <form action="login.do" method=post> 账号:< ...
- Dice
Dice http://acm.hdu.edu.cn/showproblem.php?pid=5012 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- .net VS2008 时间加减,时间段,时间格式化到秒
举个例子: DateTime time1 = DateTime.Now; DateTime time2 = time1.AddDays(1); time1是当前时间,time2比当前时间多一天.也就是 ...
- Sql自定义表类型批量导入数据
-- 创建自定义表类型 CREATE TYPE [dbo].[App_ProductTable] AS TABLE( [p_name] [varchar](50) NOT NULL, [p_audio ...
- asp.net core web 本地iis开发
发布后打开没有问题,直接配置到本地iis时,会提示如下错误 HTTP Error 502.5 - Process Failure
- 团队项目:二次开发--v.2.1--软件工程
原先代码,对于基本对象的Get,Set方法构造函数等方法与实现基本功能的方法统一放到了一起,容易造成代码不清晰,别人比较难阅读的情况.而且其中代码冗余比较多. 改进代码,进行了层次的分析,将基本对象与 ...
- PAT 1015 德才论 (25)(代码+思路)
1015 德才论 (25)(25 分)提问 宋代史学家司马光在<资治通鉴>中有一段著名的"德才论":"是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子, ...
- Debian 利用 iso 镜像完全离线更新 apt-cdrom
1 目的 在日常的 linux 服务器管理中,出于某些考虑,服务器要求与 Internet 完全隔离. 这使得我们对系统的更新和软件包的升级感到无比头疼. 下面介绍的这种方法,采用 ISO 文件,进行 ...
- Jmeter中的XPath Assertion
XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历. XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 XPointe ...