LeetCode 151 reverse word in a string

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.

这题遇到的困难主要是细节地方处理的不好:(1)空格的处理,(2)标点符号reverse?

主要思路:(1)万事开头难,你必须首先就把一些空串,单个字符串等一些在后面会出现bug的输入处理掉!!!

(2)把第一步处理好之后,将整个字符串翻转过来,标点当成字符处理。

(3)每个单词翻转,并用一个string存储。然后补充一个空格,直到处理完整个串。

(4)判断字符串是否为空(如果前面输入多个空格,到这里得到一个空字符串),非空则删除最后一个空格。

(5)输出结果。

在单词翻转的时候,使用布尔型wordHead记录是否是单词头而不是空格,确保只针对单词翻转。

class Solution {
public:
    void reverseWords(string &s)
{
    int n = s.size() - 1;
    int wHead = 0;
    int wTail = n ;
    char cPunct = ‘ ‘;
    if(s.size() == 0)
    {
        return;
    }
    if( n == 0 )
    {
        if(s[0] == ‘ ‘)
        {
            s.clear();
        }
        return;
    }

    while( wHead < wTail )
    {
        char temp = s[wTail];
        s[wTail] =  s[wHead];
        s[wHead] = temp;
        wHead++;
        wTail--;
    }

    wTail = wHead = 0;
    string sRet;
    bool wordHead = true;
    while(wTail <= n)
    {
        if(!isspace(s[wTail]) && wordHead)
        {
            wHead = wTail;
            wordHead = false;
        }
        if( ( s[wTail] == ‘ ‘ || wTail == n) && !isspace(s[wHead]))
        {
            if(wTail == n && !isspace(s[wTail]))
                wTail++;
            sRet  += SwapWord(s, wHead, wTail - 1) + " ";
            wHead =  wTail + 1;
            wordHead = true;
         }
        wTail++;
    }
    s = sRet;
    if(s.size() > 0)
    s.erase(s.size()-1, 1);

}
 string SwapWord(string &s, int wHead, int wTail)
 {
     int wStart = wHead;
     int wEnd = wTail;

     while( wStart < wEnd )
     {
         char temp = s[wStart];
         s[wStart] = s[wEnd];
         s[wEnd] = temp;
         wStart++;
         wEnd--;
     }
    return  s.substr(wHead, wTail - wHead + 1);
 }

};

应该认真的对待OJ上自己力所能及的每一题,多思考!

时间: 2024-11-05 14:46:07

LeetCode 151 reverse word in a string的相关文章

[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", 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.

leetCode 151. Reverse Words in a String 字符串反转 | Medium

151. Reverse Words in a String Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". 题目大意: 输入一个字符串,将单词序列反转. 思路1: 采用一个vector,来存放中间结果 将vector的结果倒序放入原字符串中. 思路2: 在字符串分割的时候

[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",return "blue is sky the". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. Clarification: What constitutes a

leetcode 151. Reverse Words in a String --------- java

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. 倒序字符串. 注意使用BufferString,因为如果使用Stri

Leetcode 151. Reverse Words in a String 解题报告

[Problem] 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. [Idea] 从左往右遍历一次,每当遇到空格暂停

Java for 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", return "blue is sky the". 解题思路: 本题方法多多,最简单的方式直接按“ ” spilt即可,JAVA实现如下: public String reverseWords(String s) { if (s == null || s.length()

151. Reverse Words in a String &amp;&amp; 61. Rotate List &amp;&amp; 189. Rotate Array

151. Reverse Words in a String Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". What constitutes a word?A sequence of non-space characters constitutes a word. Cou

LeetCode OJ1:Reverse Words in a String

问题描述: Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". 解题思路: 先利用split()方法将句子按空格分为几个单词的列表, 然后reverse()函数使列表逆序, 最后join函数以空格为间隔拼成字符串返回结果. Python代码: class Solution:  

LeetCode OJ - Reverse Words in a String

题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". 解题思路: 1.先对字符串进行一次总的翻转 2.以空格为单位,划分单词,然后对每个单词再进行一次翻转 代码: class Solution { public: void swap(char& a, char