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

C++實現(xiàn)LeetCode(68.文本左右對齊)

 更新時間:2021年07月17日 08:40:58   作者:Grandyang  
這篇文章主要介紹了C++實現(xiàn)LeetCode(68.文本左右對齊),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下

[LeetCode] 68.Text Justification 文本左右對齊

Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified.

You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly maxWidthcharacters.

Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.

For the last line of text, it should be left justified and no extraspace is inserted between words.

Note:

  • A word is defined as a character sequence consisting of non-space characters only.
  • Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
  • The input array words contains at least one word.

Example 1:

Input:
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
Output:
[
"This    is    an",
"example  of text",
"justification.  "
]

Example 2:

Input:
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
Output:
[
"What   must   be",
"acknowledgment  ",
"shall be        "
]
Explanation: Note that the last line is "shall be    " instead of "shall     be",
because the last line must be left-justified instead of fully-justified.
Note that the second line is also left-justified becase it contains only one word.

Example 3:

Input:
words = ["Science","is","what","we","understand","well","enough","to","explain",
"to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
Output:
[
"Science  is  what we",
"understand      well",
"enough to explain to",
"a  computer.  Art is",
"everything  else  we",
"do                  "
]

我將這道題翻譯為文本的左右對齊是因為這道題像極了word軟件里面的文本左右對齊功能,這道題我前前后后折騰了快四個小時終于通過了OJ,完成了之后想著去網(wǎng)上搜搜看有沒有更簡單的方法,搜了一圈發(fā)現(xiàn)都差不多,都挺復(fù)雜的,于是乎就按自己的思路來說吧,由于返回的結(jié)果是多行的,所以我們在處理的時候也要一行一行的來處理,首先要做的就是確定每一行能放下的單詞數(shù),這個不難,就是比較n個單詞的長度和加上n - 1個空格的長度跟給定的長度L來比較即可,找到了一行能放下的單詞個數(shù),然后計算出這一行存在的空格的個數(shù),是用給定的長度L減去這一行所有單詞的長度和。得到了空格的個數(shù)之后,就要在每個單詞后面插入這些空格,這里有兩種情況,比如某一行有兩個單詞"to" 和 "a",給定長度L為6,如果這行不是最后一行,那么應(yīng)該輸出"to   a",如果是最后一行,則應(yīng)該輸出 "to a  ",所以這里需要分情況討論,最后一行的處理方法和其他行之間略有不同。最后一個難點就是,如果一行有三個單詞,這時候中間有兩個空,如果空格數(shù)不是2的倍數(shù),那么左邊的空間里要比右邊的空間里多加入一個空格,那么我們只需要用總的空格數(shù)除以空間個數(shù),能除盡最好,說明能平均分配,除不盡的話就多加個空格放在左邊的空間里,以此類推,具體實現(xiàn)過程還是看代碼吧:

class Solution {
public:
    vector<string> fullJustify(vector<string> &words, int L) {
        vector<string> res;
        int i = 0;
        while (i < words.size()) {
            int j = i, len = 0;
            while (j < words.size() && len + words[j].size() + j - i <= L) {
                len += words[j++].size();
            }
            string out;
            int space = L - len;
            for (int k = i; k < j; ++k) {
                out += words[k];
                if (space > 0) {
                    int tmp;
                    if (j == words.size()) { 
                        if (j - k == 1) tmp = space;
                        else tmp = 1;
                    } else {
                        if (j - k - 1 > 0) {
                            if (space % (j - k - 1) == 0) tmp = space / (j - k - 1);
                            else tmp = space / (j - k - 1) + 1;
                        } else tmp = space;
                    }
                    out.append(tmp, ' ');
                    space -= tmp;
                }
            }
            res.push_back(out);
            i = j;
        }
        return res;
    }
};

到此這篇關(guān)于C++實現(xiàn)LeetCode(68.文本左右對齊)的文章就介紹到這了,更多相關(guān)C++實現(xiàn)文本左右對齊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • openCV實現(xiàn)圖像分割

    openCV實現(xiàn)圖像分割

    這篇文章主要為大家詳細介紹了openCV實現(xiàn)圖像分割,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • C++?using?編譯指令與名稱沖突問題

    C++?using?編譯指令與名稱沖突問題

    using?編譯指令由名稱空間名和它前面的關(guān)鍵字?using?namespace?組成,它使名稱空間中的所有名稱都可用,而不需要使用作用域解析運算符,這篇文章主要介紹了C++?using?編譯指令與名稱沖突,需要的朋友可以參考下
    2022-11-11
  • C++ com編程學習詳解

    C++ com編程學習詳解

    這篇文章主要介紹了C++ COM編程的學習過程,在C++中,可以使用抽象基類來實現(xiàn)COM接口,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-09-09
  • Linux C 時間函數(shù)應(yīng)用

    Linux C 時間函數(shù)應(yīng)用

    本文是關(guān)于Linux C時間函數(shù) time_t struct tm 進行了詳細的分析介紹并有應(yīng)用實例,希望能幫到有需要的同學
    2016-07-07
  • C++實現(xiàn)ETW進行進程變動監(jiān)控詳解

    C++實現(xiàn)ETW進行進程變動監(jiān)控詳解

    ETW提供了一種對用戶層應(yīng)用程序和內(nèi)核層驅(qū)動創(chuàng)建的事件對象的跟蹤記錄機制。為開發(fā)者提供了一套快速、可靠、通用的一系列事件跟蹤特性。本文將利用ETW進行進程變動監(jiān)控,需要的可以參考一下
    2022-07-07
  • C++ 創(chuàng)建桌面快捷方式 開始菜單的實現(xiàn)代碼

    C++ 創(chuàng)建桌面快捷方式 開始菜單的實現(xiàn)代碼

    這篇文章介紹了C++ 創(chuàng)建桌面快捷方式,開始菜單的實現(xiàn)代碼,需要的朋友可以參考一下
    2013-06-06
  • 使用Qt實現(xiàn)監(jiān)聽網(wǎng)頁是否響應(yīng)并導出Excel表

    使用Qt實現(xiàn)監(jiān)聽網(wǎng)頁是否響應(yīng)并導出Excel表

    Qt導出數(shù)據(jù)到excel,方法有很多,下面這篇文章主要給大家介紹了關(guān)于使用Qt實現(xiàn)監(jiān)聽網(wǎng)頁是否響應(yīng)并導出Excel表的相關(guān)資料,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下
    2023-11-11
  • C++遺傳算法類文件實例分析

    C++遺傳算法類文件實例分析

    這篇文章主要介紹了C++遺傳算法的一個類文件,是學習遺傳算法的絕佳參考資料,需要的朋友可以參考下
    2014-08-08
  • opencv求解區(qū)域的內(nèi)接矩形

    opencv求解區(qū)域的內(nèi)接矩形

    這篇文章主要為大家詳細介紹了opencv求解區(qū)域的內(nèi)接矩形,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • C語言控制臺繪制曲線的實現(xiàn)代碼

    C語言控制臺繪制曲線的實現(xiàn)代碼

    這篇文章主要為大家詳細介紹了C語言控制臺繪制曲線的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06

最新評論

东平县| 进贤县| 邵阳县| 赤水市| 景泰县| 论坛| 陵水| 泗阳县| 子洲县| 沙坪坝区| 遂昌县| 寿阳县| 永丰县| 秦安县| 永吉县| 青阳县| 安塞县| 义乌市| 宝兴县| 会宁县| 陇川县| 霍城县| 柯坪县| 宜君县| 成安县| 林口县| 科技| 罗江县| 东源县| 虹口区| 柳林县| 平塘县| 元阳县| 贵州省| 稷山县| 喜德县| 金寨县| 石屏县| 汾阳市| 嘉善县| 大埔县|