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

这道题属于思路不难,但是很繁的题目,程序十分长,需要十分细心,很考编程功底的题目吧。

分了两个特殊情况需要额外处理的:

1 只有一个单词能容纳到一行的时候

2 结尾,最后一行的时候

都需要作为左靠齐处理。还好,功力不错了,处理完两个特殊情况之后,马上就AC了。

思路:

1 计算一行可以容纳多少个单词

2 计算这行单词间需要插入多少个空格,单词间最少插入一个空格

3 处理上面两个特殊情况

翻译为程序,OK。

[cpp]  view plain copy print ?
  1. class Solution {  
  2. public:  
  3.     vector<string> fullJustify(vector<string> &words, int L) {  
  4.         vector<string> ans;  
  5.         if (words.empty()) return ans;  
  6.   
  7.         size_t ansI = -1;  
  8.         for (size_t i = 0; i < words.size();)  
  9.         {  
  10.             size_t c = 0;  
  11.             size_t len = 0;  
  12.   
  13.             //必须有空格分开,所以要增加额外空格判断-题目特殊要求(需要问清题目要求)  
  14.             int space = 0;  
  15.             while (i+c < words.size() &&   
  16.                     len+words[i+c].length()+space <= (size_t)L)  
  17.             {  
  18.                 len += words[i+c].length();  
  19.                 c++;  
  20.                 space++;  
  21.             }  
  22.             if (c == 0) c++;  
  23.             ans.push_back(words[i]);  
  24.             ansI++;  
  25.             space = L - len;  
  26.             /* 
  27.             原来还有特别要求: 
  28. Input:  ["What","must","be","shall","be."], 12 
  29. Output: ["What must be","shall    be."] 
  30. Expected:   ["What must be","shall be.   "] 
  31.             */  
  32.             if(i+c == words.size())  
  33.             {  
  34.                 size_t r = 0;  
  35.                 while (r < c-1)  
  36.                 {  
  37.                     ans[ansI] += ' ';  
  38.                     r++;  
  39.                     ans[ansI] += words[i+r];  
  40.                 }  
  41.                 for (size_t j = 0; j < space-r; j++)  
  42.                 {  
  43.                     ans[ansI] += ' ';  
  44.                 }  
  45.                 break;  
  46.             }  
  47.             //有一个单词后面必须有空格,所以要额外处理(需要问怎么处理)  
  48.             if (c == 1)  
  49.             {  
  50.                 for (int j = 0; j < space; j++)  
  51.                 {  
  52.                     ans[ansI] += ' ';  
  53.                 }  
  54.             }  
  55.             else  
  56.             {  
  57.                 size_t j = 0, k = 0;  
  58.                 j = space/(c-1);  
  59.                 k = space%(c-1);  
  60.                 size_t r = 0;  
  61.                 while (r < c-1)  
  62.                 {  
  63.                     for (size_t index = 0; index < j; index++)  
  64.                     {  
  65.                         ans[ansI] += ' ';  
  66.                     }  
  67.                     if (k > 0)    
  68.                     {  
  69.                         ans[ansI] += ' ';  
  70.                         k--;  
  71.                     }  
  72.                     r++;  
  73.                     ans[ansI] += words[i+r];  
  74.                 }  
  75.             }  
  76.             i+=c;  
  77.         }  
  78.         return ans;  
  79.     }  
  80. };  


[cpp]  view plain copy print ?
  1. //2014-2-9 update  
  2.     vector<string> fullJustify(vector<string> &words, int L)   
  3.     {  
  4.         vector<string> rs;  
  5.           
  6.         for (int i = 0; i < words.size(); )  
  7.         {  
  8.             int j = i+1;  
  9.             int len = words[i].length();  
  10.             for (; j < words.size() && len+words[j].length()<L; j++)  
  11.                 len += 1 + words[j].length();  
  12.               
  13.             if (j == words.size())  
  14.             {  
  15.                 string s(words[i]);  
  16.                 for (i++ ; i < j; i++) s +=" "+words[i];  
  17.                 while (s.length() < L) s.push_back(' ');  
  18.                 rs.push_back(s);  
  19.                 return rs;  
  20.             }  
  21.             if (j-i == 1)   
  22.             {  
  23.                 rs.push_back(words[i++]);  
  24.                 rs.back().append(L-rs.back().length(), ' ');  
  25.                 continue;  
  26.             }  
  27.   
  28.             int a = (L-len) / (j-i-1) + 1;  
  29.             int b = (L-len) % (j-i-1);  
  30.             string s(words[i]);  
  31.             for (i++; i < j; i++, b--)  
  32.             {  
  33.                 s.append(a,' ');  
  34.                 if (b>0) s.push_back(' ');  
  35.                 s.append(words[i]);  
  36.             }  
  37.             rs.push_back(s);  
  38.         }  
  39.         return rs;  

你可能感兴趣的:(LeetCode Text Justification)