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 ...
随机推荐
- jQuery源码解读二(apply和call)
一.apply方法和call方法的用法: apply方法: 语法:apply(thisObj,[,argArray]) 定义:应用某一对象的一个方法,用另一个对象替换当前对象. 说明:如果argArr ...
- Distributing Ballot Boxes
Distributing Ballot Boxes http://acm.hdu.edu.cn/showproblem.php?pid=4190 Time Limit: 20000/10000 MS ...
- UpdatePanel中弹出新窗口
如果允许,在UpdatePanel中使用iframe即可,不允许的话,用下面的方法实现弹窗 要在UpdatePanel中使用FileUpload时,会遇到此问题,或者同类其它情况 <asp:Sc ...
- C#中如何创建xml文件 增、删、改、查 xml节点信息
XML:Extensible Markup Language(可扩展标记语言)的缩写,是用来定义其它语言的一种元语言,其前身是SGML(Standard Generalized Markup Lang ...
- [leetcode]277. Find the Celebrity 找名人
Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...
- c语言定义函数指针和typedef简写
二种方法来定义函数指针 #include<stdio.h> #include<stdlib.h> #include<Windows.h> int add(int a ...
- 四种强制类型转换的总结(const_cast、static_cast、dynamic_cast、reinterpreter_cast)
四种强制类型转换的总结(const_cast.static_cast.dynamic_cast.reinterpreter_cast) 转载 2011年10月03日 23:59:05 标签: stru ...
- 【D3D】Direct3D中LPRECT(上左右底)和LPoint(x,y)之前转换
D3DSprite.cpp void CD3DSprite::DrawText(CD3DFont *pFont, char *szString, RECT &DesRect, D3DCOLOR ...
- MySql删除表、数据
程度从强到弱 1.drop table tb drop将表格直接删除,没有办法找回 2.truncate (table) tb 删除表中的所有数据,不能与where一起使用 ...
- Apache Cordova vs Adobe PhoneGap: the differences and which one to use
http://www.makehybridapps.com/2014/06/09/cordova-vs-phonegap-the-differences-and-which-one-to-use/