68. Text Justification *HARD*

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.

For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16.

Return the formatted lines as:

[
   "This    is    an",
   "example  of text",
   "justification.  "
]

Note: Each word is guaranteed not to exceed L in length.

click to show corner cases.

Corner Cases:

  • A line other than the last line might contain only one word. What should you do in this case?
    In this case, that line should be left-justified.
class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        int n = words.size(), l, extra, eachex, i, j, k, t;
        vector<int> len(n, 0);
        for(i=0; i<n; i++)
            len[i] = words[i].length();
        vector<string> ans;
        i=0;
        while(i<n)
        {
            for(l=len[i], j=i+1; j<n && l<=maxWidth; j++)
                l += len[j]+1;
            string s = words[i];
            if(j==n && l<=maxWidth) //the last line
            {
                for(k = i+1; k<j; k++)
                {
                    s += ‘ ‘;
                    s += words[k];
                }
            }
            else
            {
                j--;
                l -= len[j]+1;
                extra = maxWidth-l;
                eachex = (j>i+1) ? extra / (j-i-1) : extra;
                for(k=i+1; k<j; k++)
                {
                    if(extra)
                    {
                        if((j == i) || (extra == eachex*(j-k)))
                        {
                            for(t=0; t<eachex; t++)
                                s += ‘ ‘;
                            extra -= eachex;
                        }
                        else
                        {
                            for(t=0; t<eachex+1; t++)
                                s += ‘ ‘;
                            extra -= eachex+1;
                        }

                    }
                    s += ‘ ‘;
                    s += words[k];
                }
            }
            for(k=s.length(); k<maxWidth; k++)
                s += ‘ ‘;
            ans.push_back(s);
            i = j;
        }
        return ans;
    }
};

测试用例:

["a","b","c","d","e"] 1 -- ["a","b","c","d","e"]

["Listen","to","many,","speak","to","a","few."] 6 -- ["Listen","to    ","many, ","speak ","to   a","few.  "]

["Here","is","an","example","of","text","justification."] 16 --

["Here    is    an","example  of text","justification.  "]

["Don‘t","go","around","saying","the","world","owes","you","a","living;","the","world","owes","you","nothing;","it","was","here","first."]
30 --

["Don‘t  go  around  saying  the","world  owes  you a living; the","world owes you nothing; it was","here first.                   "]
 
时间: 2024-08-14 07:35:48

68. Text Justification *HARD*的相关文章

LeetCode --- 68. Text Justification

题目链接: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

No.68 Text Justification

No.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

leetcode 68 Text Justification ----- java

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 ex

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 ex

【一天一道LeetCode】#68. Text Justification

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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

Leetcode 68. Text Justification 文本调整 解题报告

1 解题思想 这道题,其实我也想不通为什么要标记为Hard模式,题目的大意就是对一个字符串数组进行格式化调整,输出对应的句子. 要求有: 1.每一行的字符串长度不能超过一个固定长度maxWidth 2.每两个单词之间必须有一个空格,如果一行之间的单词之间空格不能细分,那么必须左边的空格多,右边的少.并且,空格多的地方只比右边少的多一个 3.最后一行不适用2的空格方式,正常的每个单词空一格就好,最后留白就好 提前贴个解释: * 这道题关键在于仔细的处理每一个步骤: * 1.每一行选择K的单词,K个

[leetcode] 68. Text Justification 解题报告

题目链接: https://leetcode.com/problems/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; tha

[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

【LeetCode】 -- 68.Text Justification

题目大意:给定一个数组容器,里面存有很多string: 一个int maxWith.让你均匀安排每一行的字符串,能够尽可能的均匀. 解题思路:字符串+贪心.一开始想复杂了,总觉的题意描述的不是很清楚,其实放到实际的场景中去,无非就是想让前端字符串布局变得更加美观,而设计的字符串对其方式(分散对齐)(如果每一行只有一个字符串的话,那么左对齐). 附上代码: 1 vector<string> fullJustify(vector<string>& words, int maxW