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

题意

确定最大行字符数为max,将一段文本以每行max的标准左右对齐

对于每行来说,单词之间可以用尽量均衡的空格隔开,如果空格实在均衡不了的话就左边的空格数比右边的大

如果一行只有一个单词,左对齐,右边都用空格补齐

最后一行单词之间的空格数只能是1

题解

一开始挺慢的

 1 class Solution {
 2 public:
 3     vector<string> fullJustify(vector<string> &words, int L) {
 4         vector<string> res;
 5         int i = 0;
 6         while (i < words.size()) {
 7             int j = i, len = 0;
 8             while (j < words.size() && len + words[j].size() + j - i <= L) {
 9                 len += words[j++].size();
10             }
11             string out;
12             int space = L - len;
13             for (int k = i; k < j; ++k) {
14                 out += words[k];
15                 if (space > 0) {
16                     int tmp;
17                     if (j == words.size()) { 
18                         if (j - k == 1) tmp = space;
19                         else tmp = 1;
20                     } else {
21                         if (j - k - 1 > 0) {
22                             if (space % (j - k - 1) == 0) tmp = space / (j - k - 1);
23                             else tmp = space / (j - k - 1) + 1;
24                         } else tmp = space;
25                     }
26                     out.append(tmp, ' ');
27                     space -= tmp;
28                 }
29             }
30             res.push_back(out);
31             i = j;
32         }
33         return res;
34     }
35 };
View Code

后来循环加空格的操作用append函数代替后就快了一半

 1 class Solution {
 2 public:
 3     vector<string> fullJustify(vector<string>& words, int maxWidth) {
 4         int n = words.size(), i = 0, sum = -1, last = 0;
 5         vector<string>res;
 6         while (i < n) {
 7             int newsum = 1 + words[i].length() + sum;
 8             if (newsum > maxWidth && i-last>1) {
 9                 int lack = maxWidth - sum;
10                 int every = lack / (i - last - 1), extra = lack % (i - last - 1), j;
11                 string ans = "";
12                 for (j = 0; j < extra; j++) {
13                     ans += words[last + j];
14                     if (j != i - last - 1)
15                         ans.append(every + 2, ' ');
16                 }
17                 for (; j < (i-last); j++) {
18                     ans += words[last + j];
19                     if (j != i - last - 1) 
20                         ans.append(every + 1, ' ');
21                 }
22                 sum = -1; last = i;
23                 res.push_back(ans);
24             }
25             else if (newsum > maxWidth) {
26                 string ans = "";
27                 ans += words[last];
28                 int lack = maxWidth - ans.length();
29                 ans.append(lack, ' ');
30                 sum = -1; last = i;
31                 res.push_back(ans);
32             }
33             else if (i == n - 1) {
34                 string ans = "";
35                 ans += words[last];
36                 for (int j = last + 1; j <= i; j++)
37                     ans += " " + words[j];
38                 int lack = maxWidth - ans.length();
39                 ans.append(lack, ' ');
40                 res.push_back(ans);
41                 i++;
42             }
43             else {
44                 sum = newsum;
45                 i++;
46             }
47         }
48         return res;
49     }
50 };
View Code
posted @ 2019-02-12 21:46  TobicYAL  阅读(158)  评论(0编辑  收藏  举报