【LeetCode】Reverse Words in a String 反轉字符串中的單詞

一年沒有管理博客園了,說來實在慚愧。。ide

最近開始刷LeetCode,以前沒刷過,說來也實在慚愧。。。函數

剛開始按 AC Rates 從簡單到難刷,以爲略無聊,就決定按 Add Date 刷,之後也可能看心情隨便選題…(⊙o⊙)…今天作了14年 Add 的三個題,其中 Maximum Product Subarray 着實把我折騰了好一下子,因此想一想仍是跟你們分享一下個人解法,也或許有更好的方法,期待你們的分享。就把三個題按 Add Date 的前後順序分享一下吧。post

Add Date 2014-03-05spa

Reverse Words in a String3d

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

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

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.

單純把 "the sky is blue" reverse 成 "blue is sky the" 是件很容易的事情,也是比較經典的題目,不過本題說:ip

1. s 中的開頭和結尾有可能有空格,reverse 後的 string 中要去掉;leetcode

2. 連續多個空格要變成一個空格。rem

個人 code 略折騰,用了兩個函數:

1. reverseWord 實現最基本的 reverse,首先整個 reverse,變爲「eulb si yks eht」,而後每一個 word reverse,遍歷兩邊便可。

2. removeSpace 實現檢查和刪除空格。遍歷一遍便可。

複雜度O(n).

附 code,僅供參考。

 1 class Solution {
 2 public:
 3     void reverseWord(string &s) {                       //反轉字符串
 4         string::iterator pre = s.begin();
 5         string::iterator post = s.end()-1;
 6         char tmp;
 7         while(pre < post) {                             //反轉整個字符串
 8             tmp = *pre;
 9             *pre = *post;
10             *post = tmp;
11             ++pre;
12             --post;
13         }
14         pre = s.begin();
15         post = pre;
16         while(post != s.end()) {                        //反轉每一個單詞
17             while(pre != s.end() && *pre == ' ') {
18                 ++pre;
19             }
20             if(pre == s.end())
21                 return;
22             post = pre;
23             while(post != s.end() && *post != ' ') {
24                 ++post;
25             }
26             --post;
27             if(post != s.end() && pre < post) {
28                 string::iterator p1 = pre;
29                 string::iterator p2 = post;
30                 while(p1 < p2) {
31                     tmp = *p1;
32                     *p1 = *p2;
33                     *p2 = tmp;
34                     ++p1;
35                     --p2;
36                 }
37             }
38             ++post;
39             pre = post;
40         }
41     }
42     
43     void removeSpace(string &s) {                       //檢查和刪除空格
44         string::iterator pre = s.begin();
45         string::iterator post = pre;
46         while(pre != s.end() && *pre == ' ') {          //刪除開頭的空格
47             s.erase(s.begin());
48             pre = s.begin();
49         }
50         if(pre == s.end())
51             return;
52         post = pre;
53         while(post != s.end()) {                        //把連續多個空格變爲一個
54             while(pre != s.end() && *pre != ' ')
55                 ++pre;
56             if(pre == s.end())
57                 return;
58             post = pre+1;
59             while(post != s.end() && *post == ' ') {
60                 s.erase(post);
61                 post = pre+1;
62             }
63             ++pre;
64         }
65         if(!s.empty()) {                                //若是最後有空格則刪除
66             pre = s.end()-1;
67             if(*pre == ' ')
68                 s.erase(pre);
69         }
70     }
71     
72     void reverseWords(string &s) {
73         reverseWord(s);
74         removeSpace(s);
75     }
76 };
View Code
相關文章
相關標籤/搜索