[LeetCode] 68. Text Justification

[LeetCode] 68. Text Justification


Given an array of words and a length L, format the text such that each line has exactly L 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 L 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.


輸入一組詞和一個長度L,按照下面的要求去調整這組詞。

  • 每一行要被儘可能多的詞填充,最長長度爲L。
  • 詞與詞之間需要空格隔開。
  • 每行不足L長度的用空格補全,補全方法爲儘可能均勻地分佈在間隔之間,實在不能均勻的話保證左邊多右邊少的規則。
  • 最後一行只要詞與詞之間間隔是一個空格,最後補足空格即可。

思路:直接硬爆。

  1. 定義一個計數器c,下標的記錄表idxs,每次c加上一個詞的長度,如果當前詞不是idxs的第一個,c還要加上間隔1,直到c>L後,去掉當前詞,然後idxs就是這一行所包含的詞。
  2. 對每一行的詞,計算還需要填補多少個空格,然後儘可能平均地填補上到間隔上。
  3. 如果當前行只有一個詞,那麼特殊考慮。

class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        if (maxWidth <= 1) {
            return words;
        }
        vector<string> res;
        vector<int> idxs;
        int len = words.size();
        int cnt = 0;
        for (int i=0; i<len; ++i) {
            if (cnt != 0) {
                cnt += 1;
            }
            cnt += words[i].length();
            idxs.push_back(i);
            if (cnt > maxWidth) {
                idxs.pop_back();
                cnt -= words[i].length() + 1;
                int wid_num = maxWidth - cnt + idxs.size() - 1;  // 還需要填的空格數
                int sepe = idxs.size()-1;
                vector<int> width_nums(sepe);
                // 計算每個間隔的空格數
                while (sepe >= 1) {
                    width_nums[sepe-1] = wid_num / sepe;
                    wid_num -= width_nums[sepe-1];
                    --sepe;
                }
                string s = "";
                int isize = idxs.size();
                for (int j=0; j<isize-1; ++j) {
                    s += words[idxs[j]];
                    int sp_num = width_nums[j];
                    while (sp_num--) {
                        s += " ";
                    }
                }
                s += words[idxs[isize-1]];
                // 特殊情況:只有一個詞。
                if (isize == 1) {
                    int sp_num = maxWidth - s.length();
                    while (sp_num--) {
                        s += " ";
                    }
                }
                res.push_back(s);
                cnt = 0;
                idxs.clear();
                --i;
            }
        }
        // 最後一行
        string s = "";
        for (int j=0; j<idxs.size()-1; ++j) {
            s += words[idxs[j]];
            s += " ";
        }
        s += words[idxs[idxs.size()-1]];
        int sp_num = maxWidth - s.length();
        while (sp_num--) {
            s += " ";
        }
        res.push_back(s);
        return res;
    }
};
發佈了35 篇原創文章 · 獲贊 0 · 訪問量 4070
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章