[LeetCode] 68. Text Justification

来源:互联网 发布:外研版英语动画软件 编辑:程序博客网 时间:2024/06/06 08:49

[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;    }};
0 0