iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 30
1
Software Development

LeetCode30系列 第 30

[LeetCode30] Day30 - END

  • 分享至 

  • xImage
  •  

教授說12點前要看到實驗結果,但我組長看我不先發文,也想把我殺了,人真難做,我只好先來發文QQ

心得

經過這30天,說長不長,說短不短,感謝隊友的激勵,讓我能每天都在與時間賽跑中發文。
從中學習了一些演算法,動了腦,雖然我的介紹與解釋不佳,但還是有人願意撥空看一下,萬分感激!!~
雖然說是最後一天,組長跟我說打打心得就好,但我還是想放個題目。

題目 68. Text Justification

下面是原文,簡單就是一行可以這麼多字 (maxWidth),知道word的左右對齊吧,阿要盡量能塞字就塞字在同行,但不能超過maxWidth
給你一個array,儲存word,依序組成文本。

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 maxWidth characters.

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 extra space 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.

解釋 & Code

這裡採用我覺得挺暴力的方式,i是words array的index。在一個while中,我們先用另一個j,是指向i下個單字,並將單字的長度加起來(sum),沒超過就繼續往後。阿要記得多加1(單字間的空格也算1字元)。
然後就將這些單字組成一個string並儲存進ans array(即完成一行),如果已經到最後一行了,一樣要填滿空白哦~

class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        vector<string> ans;
        int i = 0; //words pointer
        int spaceNum = 0;
        int spaceNumMod = 0;
        bool last = false;
        while (i < words.size()) {
            int sum = words[i].length();
            int j = i + 1;
            while (j < words.size() && sum + words[j].length() + 1 <= maxWidth) {
                sum += (words[j].length() + 1);
                j++;
            }
            j--;
            if (j >= words.size() - 1) {
                last = true;
            }
            int gap = j - i;
            int spaceNum = 0;
            int spaceNumMod = 0;
            if (gap != 0) {
                sum -= gap;
                spaceNum = (maxWidth - sum) / gap - 1;
                spaceNumMod = (maxWidth - sum) % gap;
            }
            string tmp = "";
            string space(spaceNum, ' ');
            while (i < j) {
                tmp += (words[i] + " ");
                if (!last) {
                    tmp += space;
                }
                if (!last && spaceNumMod > 0) {
                    tmp += " ";
                    spaceNumMod--;
                }
                i++;
            }
            tmp += words[j];
            if (tmp.length() != maxWidth) {
                tmp += string(maxWidth-tmp.length(), ' ');
            }
            ans.push_back(tmp);
            i++;
        }
        return ans;
    }
};

上一篇
[LeetCode30] Day29 - 432. All O`one Data Structure
系列文
LeetCode3030
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言