1. 程式人生 > >【LeetCode】145.Text Justification

【LeetCode】145.Text Justification

題目描述(Hard)

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                  "
]

演算法分析

累加單詞長度,記錄單詞間間隙,超過指定長度前,對每個間隙補齊空格;對最後一行,自己在尾部補齊不足的單詞。

提交程式碼:

class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        vector<string> result;
        int begin = 0, len = 0;
        for (int i = 0; i < words.size(); ++i) {
            if (len + words[i].size() + (i - begin) > maxWidth) {
                result.push_back(connect(words, begin, i - 1, maxWidth, 
                                         maxWidth - len, false));
                begin = i;
                len = 0;
            }
            len += words[i].size();
        }
        
        result.push_back(connect(words, begin, words.size() - 1 , maxWidth, 
                                 maxWidth - len, true));
        return result;
    }
    
private:
    /**
    words:字元陣列; begin:起始字元; end:結束字元; lastline:是否最後一行;
    maxWidth:每行字元數
    **/
    string connect(vector<string>& words, int begin, int end, int maxWidth, 
                   int L, bool lastline) {
        string s;
        // 包含字元數
        int n = end - begin + 1;
        for (int i = 0; i < n; ++i) {
            s += words[begin + i];
            add_spaces(s, i, n - 1, L, lastline);
        }
        if (s.size() < maxWidth) s.append(maxWidth - s.size(), ' ') ;
        return s;
    }
    /**
    s:需要後面加空格的字元; i間隙的索引; n空隙數; L需要新增的字元數; 
    lastline:是否最後一行;
    **/
    void add_spaces(string& s, int i, int n, int L, bool lastline) {
        if (n < 1 || i > n - 1) return;
        int spaces = lastline ? 1 : (L / n + (i < L % n ? 1 : 0));
        s.append(spaces, ' ') ;
    }
};