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 ...
随机推荐
- getchar()、putchar()、gets()、puts()、cin.get()、cin.getline()、getline()
1.getchar: 原型为int getchar(void). 它从stdin里读取一个字符.返回值为用户输入的ASCⅡ码,出错返回-1. eg:c=getchar(). 2.putchar: 原型 ...
- 89. Gray Code (Bit)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 可重入函数reentrant function
可重入函数主要用于多任务环境中,一个可重入的函数简单来说就是可以被中断的函数:而不可重入的函数由于使用了一些系统资源,比如全局变量区,中断向量表等,所以它如果被中断的话,可能会出现问题,这类函数是不能 ...
- Python并发讨论
手段有多线程,多进程,协程. 对于多线程: 由于GIL(全局解释器锁)的存在,多线程实际是单线程的,不能发挥多核的作用: 但对于IO密集型程序,多线程对于效率是有提高的,由于阻塞时,可能会切换到别的线 ...
- Excel日期格式调整
3-Aug-2008 自定义格式: [$-809]d-mmm-yyyy;@ Aug-2008 自定义格式: [$-809]mmm-yyyy;@
- Centos7下安装apache2.4 php5.6 pdo_oci oci8
一.下载必须的安装源码包 http://httpd.apache.org/download.cgi#apache24 httpd-2.4.23.tar.gz http://apr.apache.org ...
- TEXTMETRICW 结构记录
if( flags == DT_RIGHT ) { SIZE Size = {,}; TEXTMETRICW temp; if (font->GetTextMetricsW(&temp) ...
- 如何定义一个高逼格的原生JS插件
插件的需求 我们写代码,并不是所有的业务或者逻辑代码都要抽出来复用.首先,我们得看一下是否需要将一部分经常重复的代码抽象出来,写到一个单独的文件中为以后再次使用.再看一下我们的业务逻辑是否可以为团队服 ...
- winobj
查看系统对象 windbg+pdb符号信息 查询各个系统的内核对象结构
- html 5 如何限制上传的文件类型 (uploadifive)
可以直接设置input标签的accept属性来限制上传文件的类型 <input type="file" accept="application/msword&quo ...