leetcode - Reverse Words in a String

原创 2015年07月07日 15:59:20

题意:

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.

思路:先将字符串中连续空格删除,不同单词之间仅保留一个空格。同时将开始的空格删除,结尾的空格删除。

         然后将字符串进行就地逆置,将字符串的各个单词进行就地逆置。

       

class Solution {
public:
    void delete_kong(string &s)
    {
        int i = 0;
        int len = s.length();
        int index = 0;
        for(i = 0;i<len;i++)
        {
            if(s[i] == ' ')
            continue;
            else
            break;
        }
        int j = i;
        for(j = i;j<len;j++)
        {
            if(s[j] == ' ' && j+1<len && s[j+1] == ' ')
             {
                 continue;
             }
             else
            {
                s[index] = s[j];
                index++;
            }
        }
        if(s[index-1] == ' ')
            index--;
        s = s.substr(0,index);

    }
    void reverse(string &s,int start,int end)
    {
        int i = 0;
        int mid = (end-start)/2;
        for(i = 0;i<=mid;i++)
        {
            char temp = s[start+i];
            s[start+i]= s[end-i];
            s[end-i] = temp;
        }
    }
    void reverseWords(string &s) {

        int i = 0;
        if(s == " " || s== "")
          s = "";
        delete_kong(s);

        int len = s.length();
        int start = 0;
        int end = len-1;
        reverse(s,start,end);
        //cout<<s<<endl;

        for(i = 0;i<len;i++)
        {
            if(s[i] == ' ')
            {

                if((i-1)>=0)
                {
                    end = i-1;
                    reverse(s,start,end);
                    start = i+1;
                    end = i+1;
                }
            }
        }
        end = len-1;
        reverse(s,start,end);
        }

};

        

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

将一个英文句子单词顺序逆置(LeetCode | Reverse Words in a String)

题目: Given an input string, reverse the string word by word. For example, Given s = "the sky...

[LeetCode186]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...

[leetcode]151. Reverse Words in a String

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

LeetCode(151) Reverse Words in a String

题目Given an input string, reverse the string word by word.For example, Given s = “the sky is blue”, ...

《leetCode》:Reverse Words in a String

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

LeetCode之186_Reverse Words in a String 字符串词序反转算法

给定一个字符串S,反转其中存在的单词顺序,将

[leetcode]151. Reverse Words in a String@Java解题报告

https://leetcode.com/problems/reverse-words-in-a-string/tabs/description Given an input s...

LeetCode OJ 之 Reverse Words in a String (翻转字符串的单词)

题目: Given an input string, reverse the string word by word.(给定一个输入字符串,按单词翻转这个字符串) For exampl...

LeetCode 之 Reverse Words in a String — C 实现

Reverse Words in a String   Given an input string, reverse the string word by word. For example, ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)