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.

反转string里面的单词顺序。

注意在Clarification里头:

一个单词中不包含空格,字符串开始和结尾可以有若干个空格符号,单词与单词之间的空格可以有一个或多个空格符号,我们需要把他变成一个空格。

解题思路:

我是这样想的,把s中的每一个单词都分割出来存储到一个vector里面去,再让这个vector从后往前遍历,result字符串就是题目中要求的格式了,然后赋值给s就行了。

首先,如何去掉字符串s首尾的空格呢?

通过string的find_first_not_of()和find_last_not_of()方法,分别用begin和end指向第一个单词的开始和最后一个单词的结尾处,接下来,只需要在begin和end范围里进行for循环就行了。

那~怎么判断我当前的 i 是在单词里面还是在单词外面呢?

设置一个bool 变量 betweenWord,当betweenWord为true表示在单词内部,betweenWord为false时在单词外面。

当从单词内部到下一个空格字符时,通过substr获得第一个单词,然后设置betweenWord为false,当下一个字符不是空格时候,说明进入了单词内部了,用cur指向该单词的第一个位置,然后继续遍历直到下一个空格处,说明不在单词内部了,我们获得另一个sub单词,存进vector。

最后,用result字符串保存符合格式的字符串,再赋值给s。

代码如下:

class Solution {
public:
void reverseWords(string &s) {
size_t begin = s.find_first_not_of(" ");
if(begin == string::npos){
//s中全都是空格符号
s = "";
return ;
}
size_t end = s.find_last_not_of(" ");
bool betweenWord = true;
string sub,result;
size_t cur = begin;
vector<string> vec; for(int i = begin; i <= end; i++){
if(betweenWord){
if(s[i] == ' ' || i == end){
//进入到单词外部
if(i == end){
sub = s.substr(cur,i-cur+);
vec.push_back(sub);
}
else{
sub = s.substr(cur,i-cur);
vec.push_back(sub);
}
betweenWord = false;
}
}
else{
if(s[i] != ' '){
//进入单词内部
if(i == end){
//最后一个单词只有一个字符的情况
sub = s.substr(i,);
vec.push_back(sub);
}
cur = i;
betweenWord = true;
}
}
} for(int i = vec.size()-; i > ; i--){
result += vec[i] + " ";
}
result += vec[]; s = result;
}
};

哎,我写了那么多,别人用十几行就搞定了,这就是差距啊……

Orz……

    void reverseWords(string &s)
{
string rs;
for (int i = s.length()-; i >= ; )
{
while (i >= && s[i] == ' ') i--;
if (i < ) break;
if (!rs.empty()) rs.push_back(' ');
string t;
while (i >= && s[i] != ' ') t.push_back(s[i--]);
reverse(t.begin(), t.end());
rs.append(t);
}
s = rs;
}

【LeetCode练习题】Reverse Words in a String的更多相关文章

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

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

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

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

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  4. Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

    题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...

  5. 【LeetCode】Reverse Words in a String 反转字符串中的单词

    一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...

  6. leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String

    557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...

  7. leetcode - [1]Reverse Words in a String

    Question: Reverse Words in a String Given an input string, reverse the string word by word. For exam ...

  8. LeetCode 345. Reverse Vowels of a String

    Write a function that takes a string as input and reverse only the vowels(元音字母) of a string. Example ...

  9. 【leetcode】Reverse Words in a String

    今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...

  10. Python [Leetcode 345]Reverse Vowels of a String

    题目描述: Write a function that takes a string as input and reverse only the vowels of a string. Example ...

随机推荐

  1. SQL Interview Question

    面试的时候发现会问一些SQL的基本问题,在此总结一下. ProgramInterview/SQL 这个网站上的问题还比较全. 1. Join type INNER JOIN: Returns all ...

  2. Android之字符串的拆分-split

    字符串的拆分可以利用android的 split 来简单实现 具体看如下代码: String s3 = "Real-How-To"; String [] temp = null; ...

  3. c++面试知识点

    static #include<stdio.h> #include<iostream> #include<assert.h> using namespace std ...

  4. 迭代 Iterate

    迭代:指按照某种顺序逐个访问列表中的每一项.比如:for语句 逐个访问: lst = ['q', 'i', 'w', 's', 'i', 'r'] for i in lst: print (i), # ...

  5. Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.3

    Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.3 http://blog.csdn.net/sunbow0 第二章Deep ...

  6. 用CSS3实现对图片的放大效果

    用CSS3对图片放大效果 .right_div .topicons li a:hover img{     -webkit-transform:scale(1.5,1.5);     -moz-tra ...

  7. boost 定时器.

    #include <iostream> #include <boost/asio.hpp> int main() { boost::asio::io_service io; b ...

  8. linux 系统下配置安装 java jdk 图文流程

    先查看一下系统版本,本例采用的操作系统是CentOS 6.5: 如果你是初装之后的操作系统,那么有可能wget这个组件是不存在的,所以你要安装一下它,这样才可以让你从网上down下你要的安装包: 上面 ...

  9. C# WinFrom 导入Excel文件,显示进度条

    因为WINForm程序是在64位上运行如果使用另外一种快速的读取Excel的方法会报“未在本地计算机上注册“Microsoft.Jet.OLEDB.12.0”提供程序” 所以我就换了现在这种读取有点慢 ...

  10. js Range

    http://www.zhangxinxu.com/wordpress/2011/04/js-range-html%E6%96%87%E6%A1%A3%E6%96%87%E5%AD%97%E5%86% ...