题目:

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

click to show clarification.

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) {
if(s == "") return;
if(s.length() == popBlankLead(s)) return;
else if(popBlankTrail(s) < 0) return;
int judge = 0;
while(judge < s.length() && s[judge] != ' ') ++judge;
if(judge == s.length()) return; reverseAlpha(s, 0, s.length() - 1);
int start = 0;
for(int end = 0; end < s.length(); ++end){
if(s[end] == ' '){
while(s[end + 1] == ' '){
s.erase(end, 1);
}
reverseAlpha(s, start, end - 1);
start = end + 1;
}
}
reverseAlpha(s, start, s.length() - 1);
}
void reverseAlpha(string &s, int begin, int end){
if(s == "" || begin >= end) return;
int mid = (end + begin +1) >> 1;
for(int i = begin; i < mid; ++i){
char c = s[i];
s[i] = s[end];
s[end--] = c;
}
}
int popBlankLead(string &s){
int first = 0;
while(s[first] == ' ' && first < s.length()) ++first;
s.erase(0, first);
return first;
}
int popBlankTrail(string &s){
int end = s.length() - 1;
while(s[end] == ' ' && end >= 0) --end;
s.erase(end + 1,s.length() - end - 1);
return end;
}
};

精简版代码:

class Solution {
public:
void reverseWords(string &s) {
string buf;
stringstream ss(s);
vector<string> tokens;
while (ss >> buf) tokens.push_back(buf);
if (tokens.size() == 0) s="";
else{
int n = tokens.size()-1;
s = tokens[n];
for (int i = n-1; i >=0; -- i) s+=" "+tokens[i];
}
}
};

7. Reverse Words in a String的更多相关文章

  1. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

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

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

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

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

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

  5. LeetCode Reverse Words in a String II

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...

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

  7. leetcode6 Reverse Words in a String 单词取反

    Reverse Words in a String  单词取反 whowhoha@outlook.com Question: Given an input string s, reverse the ...

  8. leetcode面试准备:Reverse Words in a String

    leetcode面试准备:Reverse Words in a String 1 题目 Given an input string, reverse the string word by word. ...

  9. 345. Reverse Vowels of a String(C++)

    345. Reverse Vowels of a String Write a function that takes a string as input and reverse only the v ...

  10. 【LeetCode练习题】Reverse Words in a String

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

随机推荐

  1. 变量声明提升 Vs. 函数声明提升

    1. 变量声明提升 先看以下代码: 1)var in_window = "a" in window; console.log(in_window); 2)var in_window ...

  2. 关于VS2013中Win32程序怎么修改图标

    首先向资源文件上加上你要添加的资源(把你要添加的图标放在你的工程的下面,然后右击资源文件选中添加资源,然后选择导入你要添加的图标),下面你只要打开你项目的.rc文件要用查看代码形式打开,然后只要把系统 ...

  3. 甘特图和PERT图

    gantt图又叫甘特图.进度是按时间顺序计划活动的一个列表,我们称之为Gantt图,它有以下几个关键的成分:1.横跨图顶部排列的是日历表.2.最左边的一列包含了每项任务的标识号(ID).3.左边第二列 ...

  4. UIMenuController的使用,对UILabel拷贝以及定制菜单

    分类: ios开发2012-08-06 17:15 11961人阅读 评论(0) 收藏 举报 actionmenuuiview 1. Menu所处的View必须实现 – (BOOL)canBecome ...

  5. good books

  6. JQuery源码解析(十)

    默认回调对象设计 不传入任何参数,调用add的时候将函数add到内部的list中,调用fire的时候顺序触发list中的回调函数: function fn1(val) { console.log('f ...

  7. bigworld源码分析(5)——BaseApp分析

    BaseApp负载部分,核心代码缺失...网上的源码中都找不到,暂时没办法分析其核心内容,很遗憾,继续寻找吧,等找到了,再继续自己的分析.

  8. 移动互联网实战--资源类APP的数据存储处理和优化

    前言: 对于资源类的APP, 其音频/图形占据了APP本身很大的比例. 如何存储和管理这些资源文件, 成了一个颇具挑战性的难点. 移动端的碎片化, 高中低端手机的并存, 需要开发者不光是具备基础的存储 ...

  9. applicationContext.xml和web.xml的一些配置

    applicationContext.xml <!-- test环境 --> <beans profile="test"> <context:prop ...

  10. zoj3551 Bloodsucker ——概率DP

    Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4530 A[i]数组表示当吸血鬼有 I 个的时候,还需要的天数.可以 ...