博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]Reverse Words in a String
阅读量:4587 次
发布时间:2019-06-09

本文共 3411 字,大约阅读时间需要 11 分钟。

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

For example,

Given s = “the sky is blue”,
return “blue is sky the”.

把句子看做由词组成的,例如“A B C”,因此可以将句子的所有字符前后交换,得到“C’ B’ A’”。显然X‘表示逆序的词X,所以第二步是将每个词中的字符串前后交换。整个过程的时间复杂度为O(n),空间复杂度为O(1)。这种方法的缺点是没有考虑许多特殊情况,例如字符串中有连续的空格,字符串开始结尾处有空格等。

class Solution {  public:      void reverseWords(string &s) {          s = removeDuplicateSpace(s);        int begin = 0;          int end = 0;        while(end < s.size()){              if(s[end] == ' '){                  swapString(s, begin, end - 1);                  begin = end+1;                  end = begin;              }              else{                  end++;              }          }          swapString(s, begin, end - 1);          swapString(s, 0, s.size()-1);      }      void swapString(string &s, int begin, int end) {          while(end > begin){              char c = s[begin];              s[begin] = s[end];              s[end] = c;              begin++;              end--;          }      }    string removeDuplicateSpace(string s) {        string res;        int begin = 0;        for(; begin < s.size(); begin++) {            if(s[begin] != ' '){                break;            }        }        int end = s.size() - 1;        for(; end >= 0; end--) {            if(s[end] != ' '){                break;            }        }        bool is_space = false;        for(int i = begin; i <= end; i++) {            if(s[i] == ' '){                if(!is_space){                    is_space = true;                }                else{                    continue;                }            }            else{                is_space = false;            }            res.push_back(s[i]);        }        return res;    }};

另一种方法是,利用两个stack,一个表示单词,一个表示句子。当遇到非空格字符时放入单词stack;当遇到空格时将单词stack中的字符压入句子stack中(注意:单词此时已经逆序一次),然后仅添加一个空格。最后将句子stack依次输出,此时句子逆序。两次逆序的道理同方法1.

class Solution {  public:      void reverseWords(string &s) {          s = removeDuplicateSpace(s);                int begin = 0;          int end = 0;                  while(end < s.size()){              if(s[end] == ' '){                  swapString(s, begin, end - 1);                  begin = end+1;                  end = begin;              }              else{                  end++;              }          }          swapString(s, begin, end - 1);                    swapString(s, 0, s.size()-1);      }            void swapString(string &s, int begin, int end) {          while(end > begin){              char c = s[begin];              s[begin] = s[end];              s[end] = c;              begin++;              end--;          }      }        string removeDuplicateSpace(string s) {        string res;        int begin = 0;        for(; begin < s.size(); begin++) {            if(s[begin] != ' '){                break;            }        }        int end = s.size() - 1;        for(; end >= 0; end--) {            if(s[end] != ' '){                break;            }        }        bool is_space = false;        for(int i = begin; i <= end; i++) {            if(s[i] == ' '){                if(!is_space){                    is_space = true;                }                else{                    continue;                }            }            else{                is_space = false;            }            res.push_back(s[i]);        }        return res;    }}; 

转载于:https://www.cnblogs.com/liangf27/p/9356858.html

你可能感兴趣的文章
(20)模型层 -ORM之msql 基于双下划线的跨表查询(一对一,一对多,多对多)...
查看>>
nginx日志格式定义和nginx.conf配置模板说明
查看>>
深入DLR语言——IronJS
查看>>
Apache Solr 3.6.2 发布
查看>>
ES5新增
查看>>
Js获取当前浏览器的高和宽度
查看>>
MAC常用的快捷键
查看>>
注册码
查看>>
记录一下中间过程2
查看>>
Retrofit
查看>>
ASP.NET 开发人员不必担心 Node 的五大理由
查看>>
插入排序(1)——直接插入排序(insert sort)
查看>>
voltdb数据库持久性,扩展集群
查看>>
2018 焦作网络赛 L Poor God Water ( AC自动机构造矩阵、BM求线性递推、手动构造矩阵、矩阵快速幂 )...
查看>>
2017年终总结
查看>>
PowerPoint笔记(五)
查看>>
使用线程 Monitor.Wait() 和 Monitor.Pulse()
查看>>
c# 方法参数(传值,传引用,ref,out,params,可选参数,命名参数)
查看>>
C# FTP上传下载(支持断点续传)
查看>>
JMeter学习(一)JMeter的安装和目录解析
查看>>