题目:

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.

解题思路: 先整体翻转,在把单词一个个翻转。(或反过来也行)

代码也许还能优化:

  1. class Solution {
  2. public:
  3. void reverseWords(string &s) {
  4. if(s == "") return;
  5. if(s.length() == popBlankLead(s)) return;
  6. else if(popBlankTrail(s) < 0) return;
  7. int judge = 0;
  8. while(judge < s.length() && s[judge] != ' ') ++judge;
  9. if(judge == s.length()) return;
  10.  
  11. reverseAlpha(s, 0, s.length() - 1);
  12. int start = 0;
  13. for(int end = 0; end < s.length(); ++end){
  14. if(s[end] == ' '){
  15. while(s[end + 1] == ' '){
  16. s.erase(end, 1);
  17. }
  18. reverseAlpha(s, start, end - 1);
  19. start = end + 1;
  20. }
  21. }
  22. reverseAlpha(s, start, s.length() - 1);
  23. }
  24. void reverseAlpha(string &s, int begin, int end){
  25. if(s == "" || begin >= end) return;
  26. int mid = (end + begin +1) >> 1;
  27. for(int i = begin; i < mid; ++i){
  28. char c = s[i];
  29. s[i] = s[end];
  30. s[end--] = c;
  31. }
  32. }
  33. int popBlankLead(string &s){
  34. int first = 0;
  35. while(s[first] == ' ' && first < s.length()) ++first;
  36. s.erase(0, first);
  37. return first;
  38. }
  39. int popBlankTrail(string &s){
  40. int end = s.length() - 1;
  41. while(s[end] == ' ' && end >= 0) --end;
  42. s.erase(end + 1,s.length() - end - 1);
  43. return end;
  44. }
  45. };

精简版代码:

  1. class Solution {
  2. public:
  3. void reverseWords(string &s) {
  4. string buf;
  5. stringstream ss(s);
  6. vector<string> tokens;
  7. while (ss >> buf) tokens.push_back(buf);
  8. if (tokens.size() == 0) s="";
  9. else{
  10. int n = tokens.size()-1;
  11. s = tokens[n];
  12. for (int i = n-1; i >=0; -- i) s+=" "+tokens[i];
  13. }
  14. }
  15. };

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

    http://acm.timus.ru/problem.aspx?space=1&num=1238 DP+记忆化搜索 思路不难,关键是最优结果的储存问题,为了编写方便,直接用string储存最 ...

  2. 验证页面多个input文本的必填项

    前台页面 JS : function CheckMustWrite(){ var count = $("input[mustwrite = 'true']", document.f ...

  3. Win7下通过eclipse远程连接CDH集群来执行相应的程序以及错误说明

    最近尝试这用用eclipse连接CDH的集群,由于之前尝试过很多次都没连上,有一次发现Cloudera Manager是将连接的端口修改了,所以才导致连接不上CDH的集群,之前Apache hadoo ...

  4. Python time模块学习

    Python time模块提供了一些用于管理时间和日期的C库函数,由于它绑定到底层C实现,因此一些细节会基于具体的平台. 一.壁挂钟时间 1.time() time模块的核心函数time(),它返回纪 ...

  5. HDU 4717 The Moving Points (三分法)

    题意:给n个点的坐标的移动方向及速度,问在之后的时间的所有点的最大距离的最小值是多少. 思路:三分.两点距离是下凹函数,它们的max也是下凹函数.可以三分. #include<iostream& ...

  6. (转)初探Backbone

    (转)http://www.cnblogs.com/yexiaochai/archive/2013/07/27/3219402.html 初探Backbone 前言 Backbone简介 模型 模型和 ...

  7. 命令参数解析库JCommonder

    1.JCommander 是一个非常小的Java 类库,用来解析命令行参数. 2.参数类型:可以是任意类型,但我使用的只有 List,String. @Parameter(name="-s& ...

  8. LeetCode189——Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  9. Certificate、Provisioning Profile、App ID

    关于 Certificate.Provisioning Profile.App ID 的介绍及其之间的关系 2014-03-13 15:26 13416人阅读 评论(1) 收藏 举报   目录(?)[ ...

  10. C#—WebService

    一.qq是否在线 1.添加Web引用    qqOnlineWebService cn.com.webxml.www.qqOnlineWebService shelly1 = new NIIT1109 ...