最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實(shí)現(xiàn)LeetCode(186.翻轉(zhuǎn)字符串中的單詞之二)

 更新時(shí)間:2021年08月04日 16:07:02   作者:Grandyang  
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(186.翻轉(zhuǎn)字符串中的單詞之二),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

[LeetCode] 186. Reverse Words in a String II 翻轉(zhuǎn)字符串中的單詞之二

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

Example:

Input:  ["t","h","e"," ","s","k","y"," ","i","s"," ","b","l","u","e"]
Output: ["b","l","u","e"," ","i","s"," ","s","k","y"," ","t","h","e"]

Note: 

  • A word is defined as a sequence of non-space characters.
  • The input string does not contain leading or trailing spaces.
  • The words are always separated by a single space.

Follow up: Could you do it in-place without allocating extra space?

這道題讓我們翻轉(zhuǎn)一個(gè)字符串中的單詞,跟之前那題 Reverse Words in a String 沒(méi)有區(qū)別,由于之前那道題就是用 in-place 的方法做的,而這道題反而更簡(jiǎn)化了題目,因?yàn)椴豢紤]首尾空格了和單詞之間的多空格了,方法還是很簡(jiǎn)單,先把每個(gè)單詞翻轉(zhuǎn)一遍,再把整個(gè)字符串翻轉(zhuǎn)一遍,或者也可以調(diào)換個(gè)順序,先翻轉(zhuǎn)整個(gè)字符串,再翻轉(zhuǎn)每個(gè)單詞,參見(jiàn)代碼如下:

解法一:

class Solution {
public:
    void reverseWords(vector<char>& str) {
        int left = 0, n = str.size();
        for (int i = 0; i <= n; ++i) {
            if (i == n || str[i] == ' ') {
                reverse(str, left, i - 1);
                left = i + 1;
            }
        }
        reverse(str, 0, n - 1);
    }
    void reverse(vector<char>& str, int left, int right) {
        while (left < right) {
            char t = str[left];
            str[left] = str[right];
            str[right] = t;
            ++left; --right;
        }
    }
};

我們也可以使用 C++ STL 中自帶的 reverse 函數(shù)來(lái)做,先把整個(gè)字符串翻轉(zhuǎn)一下,然后再來(lái)掃描每個(gè)字符,用兩個(gè)指針,一個(gè)指向開(kāi)頭,另一個(gè)開(kāi)始遍歷,遇到空格停止,這樣兩個(gè)指針之間就確定了一個(gè)單詞的范圍,直接調(diào)用 reverse 函數(shù)翻轉(zhuǎn),然后移動(dòng)頭指針到下一個(gè)位置,在用另一個(gè)指針繼續(xù)掃描,重復(fù)上述步驟即可,參見(jiàn)代碼如下:

解法二:

class Solution {
public:
    void reverseWords(vector<char>& str) {
        reverse(str.begin(), str.end());
        for (int i = 0, j = 0; i < str.size(); i = j + 1) {
            for (j = i; j < str.size(); ++j) {
                if (str[j] == ' ') break;
            }
            reverse(str.begin() + i, str.begin() + j);
        }
    }
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/186

類(lèi)似題目:

Reverse Words in a String III

Reverse Words in a String

Rotate Array

參考資料:

https://leetcode.com/problems/reverse-words-in-a-string-ii/

https://leetcode.com/problems/reverse-words-in-a-string-ii/discuss/53851/Six-lines-solution-in-C%2B%2B

https://leetcode.com/problems/reverse-words-in-a-string-ii/discuss/53775/My-Java-solution-with-explanation

到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(186.翻轉(zhuǎn)字符串中的單詞之二)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)翻轉(zhuǎn)字符串中的單詞之二內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

尼木县| 镇安县| 淮安市| 甘谷县| 海口市| 朝阳县| 黎川县| 九龙坡区| 永济市| 密云县| 马鞍山市| 西平县| 汕尾市| 通化县| 阜新| 岫岩| 蓝田县| 普兰县| 陇川县| 堆龙德庆县| 新民市| 商洛市| 安仁县| 新平| 云龙县| 措勤县| 瑞金市| 通化县| 荆门市| 鹰潭市| 昭平县| 玉环县| 沧源| 遂昌县| 普格县| 读书| 西华县| 泾阳县| 玉屏| 海南省| 东台市|