码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Reverse Words in a String

时间:2015-07-06 19:30:44      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

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

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

Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.

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.

 

 

Hide Tags
 String
 
分析:
如果单词之间遇到多个空格,只能返回一个,而且首尾不能有单词,并且对C语言程序员要求空间复杂度为O(1),所以我们只能对原字符串s之间做修改,而不能声明新的字符串。
 
第一步:反正整个字符串
第二部:反正单个word
第三部:将翻转好的word放到合适的位置,这里合适的位置是指前面的不包含space的位置。
writePos  标记合适的位置,也就是将要写入的位置。
第四部:改变字符串大小
 

另外,我们在最后压入了一个space,是为了最后的word可以统一处理。


 注意这段code,是为了处理整个字符串都是space的情况。
            if( writePos > 0) //remove the latest space
                s.resize(writePos - 1);
            else
                s.resize(0);//the str only contains space

 

总之,此题是一道好体,题小乾坤大,很多细节需要处理。

 
 
AC code:
class Solution {
    private:
        void reverseWord(string &s, int left, int right)
        {
            char tmp;
            while(left < right)
            {
                tmp = s[left];
                s[left] = s[right];
                s[right] = tmp;
                left++;
                right--;
            }
        }
    public:
        void reverseWords(string &s) {
            if(s.size() == 0)
                return;

            //reverse all words
            reverseWord(s, 0, s.size() - 1);

            s.push_back( );//inorder to handler the latest part
            int size = s.size();

            int writePos = 0;//position which will be written

            int left = 0, right = 0;
            for(int i = 0; i < size; i++)
            {
                //convert multiple spaces to one space
                if(s[i] !=  )
                {
                    if(i == 0 || s[i-1] ==  )
                    {
                        left = i;
                        right = i;
                    }
                    else
                        right ++;
                }
                else //if (s[i] == ‘ ‘)
                {
                    if(i > 0 && s[i-1] !=  )
                    {
                        //cout << "left\t" << left << endl;
                        //cout << "right\t" << right<< endl;
                        reverseWord(s, left, right);

                        //move the part to writePos
                        // it means memmove
                        while(left <= right)
                        {
                            s[writePos++] = s[left++];
                        }
                        s[writePos++] =  ;//add space after the word
                    }
                }
            }

            if( writePos > 0) //remove the latest space
                s.resize(writePos - 1);
            else
                s.resize(0);//the str only contains space
        }
};

 

[LeetCode] Reverse Words in a String

标签:

原文地址:http://www.cnblogs.com/diegodu/p/4624932.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!