leetcode系列(1)Reverse Words in a String 句中单词顺序翻转

来源:互联网 发布:网络推广员怎么做 编辑:程序博客网 时间:2024/05/16 15:49

题目描述如下

Reverse Words in a String Total Accepted: 5802 Total Submissions: 41330

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

For example,

Given s = "the sky is blue",

return "blue is sky the".

咋一看就是《编程珠玑》里面的一个经典的例子,思路就是先翻转句子中每个单词,然后翻转整个句子;但是为什么ac率这么低呢,我也没有注意,直接上代码sub,被毫不留情的挂了,提示如果输入“    ”,应该输出“”,这不科学啊,我看了下classification,发现描述如下:

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.

原来是问题中还隐藏了一个字符串中的空格处理问题啊,也不怪众屌丝,因为描述中没有说清楚要去除前导和拖尾的空格以及中间的多个连续空格变一个,不过无所谓了,加一个预处理函数就行了,自己先在xcode上跑了几个测试用例,然后粘贴sub直接ac了,代码如下:

////  reverse_word_string.cpp//  leet_code////  Created by meng duan on 14-3-29.//  Copyright (c) 2014年 886524. All rights reserved.// Given an input string, reverse the string word by word.// For example,// Given s = "the sky is blue",// return "blue is sky the"./*    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. */#include "common.h"class SolutionReverseWordString {public:    typedef string::iterator ite_str_t;    void pre_process (string &str) {        string rst = "";        bool flag = true;        // remove all the pre space        ite_str_t ite;        for (ite = str.begin(); ite != str.end(); ++ite) {            if (' ' != (*ite))                break;        }        if (ite == str.end()) {            str = rst;            return;        }        ite_str_t last;        for (last = str.end(); last != str.begin(); --last) {            if (*(last-1) != ' ')                break;        }        for ( ; ite != last; ++ite) {            if (' ' != *ite) {                rst.push_back(*ite);                flag = true;            }            if (flag && (*ite == ' ')) {                rst.push_back(*ite);                flag = false;            }        }        str = rst;    }    void reverse_word (ite_str_t start, ite_str_t end) {        while (start < end) {            char tmp = *start;            *start = *end;            *end = tmp;            ++start;            --end;        }        return;    }    void reverse_word_string (string &str) {        pre_process(str);        // ignore the pre space char in the string        ite_str_t ite = str.begin();        for ( ; ite != str.end(); ++ite) {            if (' ' != (*ite))                break;        }        // if all char is space just return        if (str.end() == ite)            return;        ite_str_t s = ite;        ite_str_t e = ite + 1;           while (str.end() != s) {            while (' ' == (*s))                 ++s;            if (str.end() == s)                break;            while ((' ' != (*e)) && (e != str.end()))                e++;            reverse_word(s, e-1);            s = e;            ++e;        }        reverse_word(str.begin(),str.end()-1);    }};int main(int argc, char **argv) {    SolutionReverseWordString test_obj;    string t1 = "        ";    string t2 = "a        ";    string t3 = "        b";    string t4 = "duan meng    ";    string t5 = " duan meng is macduan ";    test_obj.reverse_word_string(t1);    cout<<"t1 is : "<<t1<<endl;    test_obj.reverse_word_string(t2);    cout<<"t2 is : "<<t2<<endl;    test_obj.reverse_word_string(t3);    cout<<"t3 is : "<<t3<<endl;    test_obj.reverse_word_string(t4);    cout<<"t4 is : "<<t4<<endl;    test_obj.reverse_word_string(t5);    cout<<"t4 is : "<<t5<<endl;    return 0;}



0 0
原创粉丝点击