题目:

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的更多相关文章

  1. 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 ...

  2. lintcode :Reverse Words in a String 翻转字符串

    题目: 翻转字符串 给定一个字符串,逐个翻转字符串中的每个单词. 样例 给出s = "the sky is blue",返回"blue is sky the" ...

  3. LeetCode刷题:Reverse Words in a String(翻转字符串中的单词)

    题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...

  4. [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 ...

  5. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  6. [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 ...

  7. 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 ...

  8. 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 ...

  9. Leetcode 344:Reverse String 反转字符串(python、java)

    Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input strin ...

随机推荐

  1. 如何使用eclipse搭建maven环境以及常见的错误

    这篇博客适合零基础学习maven,搭建maven以及运行项目,常见的错误,我会在结尾写出说明白,看是否和大家的错误一样,或者文章的括号注释部分也会写出一些注意点. 第一步:就是下载maven,以及配置 ...

  2. ECMAScript5新特性之Object.isExtensible、Object.preventExtensions

    阻止对象扩展后: 1 不能添加属性. 2 可以修改属性的值. 3 可以删除属性. 4 可以修改属性描述符. var fruit = { name : '苹果', desc : '红富士' }; // ...

  3. php反射机制学习

    PHP 5 具有完整的反射 API,可以通过反射机制来获取类,接口,函数的详细信息.例如可以通过反射api的成员属性,成员方法,命名空间的名称,检测某个类是否为抽象类等操作.(欢迎指点) 一般用途是在 ...

  4. Spring框架的事务管理之基于AspectJ的XML方式(重点掌握)

    1. 步骤一:恢复转账开发环境(转账开发环境见“https://www.cnblogs.com/wyhluckdog/p/10137283.html”) 2.步骤二:引入AOP的开发包3.步骤三:引入 ...

  5. resin-pro-4.0.53报错java.lang.Error: java.lang.ClassNotFoundException: com.caucho.loader.SystemClassLoader

    最初并未发现,笔者的系统环境变量JAVA_HOME变量设置错误 D:\develop\Java\x64\jdk1.8.0_144 #最初使用了阉割版的JDK 改成完整安装的JDK就可以 D:\deve ...

  6. eclipse缓存太重,新手最容易中招

    有4种方法,从上到下清理:

  7. 百度 ueditor 1.2.0 注意事项 ,上传文件问题

    <script type="text/javascript" src="script/ueditor/ueditor.config.js" charset ...

  8. socketpair初识

    #include <stdio.h>  #include <string.h>  #include <unistd.h>  #include <sys/typ ...

  9. Selenium安装中的一些问题及解决办法-软硕1703班3组整理分享

    非常感谢软件工程硕士1703班3组同学的热心,他们将安装Selenium过程中踩过的坑替大家填上了.希望还没有来得及踩坑的,或者掉进坑里还没爬出来的小组,能顺利跨过去这个安装的坑. 如下是原文. Se ...

  10. Activiti中23张表的含义

    1.与流程定义相关的4张表: 2.与执行任务相关的5张表: 3.与流程变量相关的2张表