[LeetCode] 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.

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

Return the formatted lines as:

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

 

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

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.
贪心,注意细节。
 1 class Solution {
 2 private:
 3     vector<string> ret;
 4 public:
 5     vector<string> fullJustify(vector<string> &words, int L) {
 6         // Start typing your C/C++ solution below
 7         // DO NOT write int main() function
 8         ret.clear();
 9         int i = 0;
10         while(i < words.size())
11         {
12             int size = 0;
13             int beg = i;
14             while(i < words.size())
15             {
16                 int newSize = size == 0 ? words[i].size() : size + 1 + words[i].size();
17                 if (newSize <= L)
18                     size = newSize;
19                 else
20                     break;
21                 i++;
22             }
23             
24             int spaceCount = L - size;
25             int everyCount;
26             if (i - 1 - beg != 0 && i != words.size())
27             {
28                 everyCount = spaceCount / (i - 1 - beg);
29                 spaceCount %= (i - 1 - beg);
30             }
31             else
32                 everyCount = 0;
33             string s;
34             for(int j = beg; j < i; j++)
35                 if (j == beg)
36                     s = words[j];
37                 else
38                 {
39                     s += ' ';
40                     for(int k = 0; k < everyCount; k++)
41                         s += ' ';
42                     if (spaceCount > 0 && i != words.size())
43                     {
44                         s += ' ';
45                         spaceCount--;
46                     }
47                     s += words[j];
48                 }
49                 
50             for(int j = 0; j < spaceCount; j++)
51                 s += ' ';
52                 
53             ret.push_back(s);
54         }
55         
56         return ret;
57     }
58 };

 

posted @ 2012-11-18 21:08  chkkch  阅读(2213)  评论(0编辑  收藏  举报