zoukankan      html  css  js  c++  java
  • [leetcode] Reverse Words in a String [1]

    一、题目:

    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.

    原题地址

    二、解题思想

    翻转字符串中的单词顺序,这是个老题目了。可是leetcode上面的要求更为严格。如:

    要求把开头和结尾的空格删除掉;

    缩减单词间的空格数为1(假设有多个空格)。

    单词若全是空格,则返回一个空字符串("").


    此题思想不难。主要是注意上面三个要求和一些细节就能够AC。
    大致分为两步:一个是常规的翻转字符串中的单词;还有一个就是想方法去掉串中的多余的单词;这两步骤的顺序能够颠倒。

    以下给出两份代码。第一个代码是先去掉多余的空格。然后在翻转;第二个代码先翻转,在去掉多余的空格。就效率上来说应该是第一个代码的效率更高。

    三、代码实现

    代码一:
    class Solution {
    public:
        void reverseWords(string &s) {
            if(s.size()<=0) return ;
            char *work = new char[s.size()+1];
            //reduce blank
            int j=0;
            for(int i=0; s[i]!=''; ++i){
                if(i>0 && s[i] == ' ' && s[i-1]!= ' ')
                    work[j++] = s[i];
                  else if(s[i] != ' ')
                      work[j++] = s[i];
              }
              if(j>0 && work[j-1]==' ')
                  work[--j] = '';
            else
                  work[j] = '';
              //reverse all string
              reverse(work, 0, j-1);
              int p= 0, i=0;
              //reverse each word
              while(i<j){
                   while(p<j && work[p]!=' ') p++;
                   reverse(work, i, p-1);
                   i = p+1;
                   p = i;               
              }
              string temp(work);
              s = temp;
        }
        
        void reverse(char *s, int beg, int end){
            while(beg < end){
                char temp = s[beg];
                s[beg++] = s[end];
                s[end--] = temp;
            }
        }
    };
    代码二:
    class Solution {
    public:
        void reverseWords(string &s) {
            int n = s.size();
            if(n<=0) return;
            //if(n==1)
            //reverse the whole string
            reverse(s, 0, n-1);
            //reverse each word
            int begin=0, end = 0;
            while(begin<n){
                while( begin< n && s[begin] == ' ') ++begin;
                end = begin;
                while( end<n && s[end] != ' ') ++end;
                reverse(s, begin, end-1);
                begin = end;
            }
            //reduce blank
            begin = 0;
            while(begin<n && s[begin] ==' ') ++begin;
            if(begin == n) {s = s.substr(0,0);return;}
            
            end = n-1;
            while(end>=0 && s[end] == ' ') --end;
            
            int start = 0;
            char pre;
            for(; begin<=end; ++begin){
                if(s[begin] != ' '){
                    s[start++] = s[begin];
                    pre = s[begin];
                }else{
                    if(pre != ' '){
                        s[start++] = ' ';
                        pre = ' ';
                    }
                }
            }
            if(start != n) s = s.substr(0, start);
        }
        
        void reverse(string &s, int begin, int end){
            char temp;
            while(begin<end){
                temp = s[begin];
                s[begin++] = s[end];
                s[end--] = temp;
            }
        }
    };

    假设你认为本篇对你有收获。请帮顶。


    另外,我开通了微信公众号--分享技术之美。我会不定期的分享一些我学习的东西.

    你能够搜索公众号:swalge 或者扫描下方二维码关注我

    (转载文章请注明出处: http://blog.csdn.net/swagle/article/details/28236933 )
查看全文
  • 相关阅读:
    BestCoder17 1001.Chessboard(hdu 5100) 解题报告
    codeforces 485A.Factory 解题报告
    codeforces 485B Valuable Resources 解题报告
    BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告
    codeforces 374A Inna and Pink Pony 解题报告
    codeforces 483B Friends and Presents 解题报告
    BestCoder15 1002.Instruction(hdu 5083) 解题报告
    codeforces 483C.Diverse Permutation 解题报告
    codeforces 483A. Counterexample 解题报告
    NSArray中地内存管理 理解
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10472404.html
  • Copyright © 2011-2022 走看看