Skip to content

68. Text Justification

难度:Hard

刷题内容

原题连接

  • https://leetcode.com/problems/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 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.
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                  "
]

˼· **- ʱ¼ä¸´ÔÓ¶È: O(n)*- ¿Õ¼ä¸´ÔÓ¶È: O(n)***

ÌâÄ¿²»ÄÑ£¬¸ù¾ÝÌâÄ¿µÄÒâ˼ÏȼÆËã³öÒ»ÐÐ×î¶àÄÜÈÝÄɶàÉÙµ¥´Ê£¬Ã¿¸öµ¥´ÊÖ®¼äÒÔÒ»¸ö¿Õ¸ñ·Ö¸î¾Í¿ÉÒÔ¼ÆËã³öÿÐÐ×î¶àÄÜÈÝÄɶàÉÙµ¥´Ê¡£½ÓÏÂÀ´¼ÆËãÿÐеÄÕýÈ·¸ñʽ¡£¼ÆËã³ö×ܹ²µÄ¿Õ¸ñÊý³ýÒÔ¼ä¸ôÊý¡£×Ü¿Õ¸ñÊý³ýÒÔ¼ä¸ôÊý£¬ÈôÓàÊýΪ0£¬ÔòÉÌΪµ¥´ÊÖ®¼äµÄ¿Õ¸ñÊý£¬ÈôÓàÊý r ²»Îª0£¬Ôò´ÓÍ·¿ªÊ¼ r ¸ö¿Õ¸ñÊý¼Ó1£¬Ê£ÏµIJ»±ä¡£×îºóÒ»ÐÐҪעÒâÿ¸öµ¥´ÊÖ®¼äµÄ¿Õ¸ñÊýΪ1£¬×Ö·ûÊý²»ÂúmaxWidth£¬Ê£ÏµÄÓÿոñ²¹³ä¡£

class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        int l = 0,r = 0,sum = 0;
        vector<string> ans;
        for(int i = 0;i < words.size();++i)
        {
            sum += words[i].length();
            if(sum <= maxWidth)
            {
                sum += 1;
                r++;
            }
            else
            {
                string temp;
                sum -= (r - l + words[i].length());
                int residue = 0, divide = (maxWidth - words[l].length());
                if(l + 1 != r)
                {
                    residue = (maxWidth - sum) % (r - l - 1);
                    divide = (maxWidth - sum - residue) / (r - l - 1);
                }
                temp.append(words[l].begin(),words[l].end());
                for(int j = l + 1;j < r - 1;++j)
                {
                    int k = residue > 0 ? divide + 1 : divide;
                    while(k)
                    {
                        temp.push_back(' ');
                        --k;
                    }
                    temp.append(words[j].begin(),words[j].end());
                    residue--;
                }
                int k = residue > 0 ? divide +1 : divide;
                while(k)
                {
                    temp.push_back(' ');
                    --k;
                }
                if(l + 1 != r)
                    temp.append(words[r - 1].begin(),words[r - 1].end());
                ans.push_back(temp);
                l = r++;
                sum = words[i].length() + 1;
            }
        }
        sum -= 1;
        string temp;
        temp.append(words[l].begin(),words[l].end());
        for(int i = l + 1;i < r;++i)
        {
            temp.push_back(' ');
            temp.append(words[i].begin(),words[i].end());
        }
        int k = maxWidth - sum;
        while(k)
        {
            temp.push_back(' ');
            --k;
        }
        ans.push_back(temp);
        return ans;
    }
};

我们一直在努力

apachecn/AiLearning

【布客】中文翻译组