[LeetCode] Text Justification words显示的排序控制

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

 

 

Hide Tags
 String
 
 
   这题有点复杂,关于word显示的排序控制,直观的就是用word 写英文文档,自动调整一行中的空格长度,达到一行中的空格尽量平均。
 
需要考虑的情况:
  • 字符串截取,比较方便的是实现代码已经截取好,不用我们截取了。
  • 如果一个word 长度超过 一行的长度怎么处理,这个测试例子中没有考虑,所以没有实现这一部分。
  • 一行中的左右端没有空格。
  • 如果行中只有一个word ,空格添加在右边。
  • 行中空格尽量平分,不够时多的在左侧。
  • 最后一行words 之间只需要一个空格间隔,末尾需要补齐空格。
  • 困难的地方是怎么算空格,一行有多少个word 好处理,word 之间多少空格就比较麻烦。

 

思路:

  1. 遍历输入words 
  2. 计算已经输入 word长度加上空格1,和已经输入word 的长度,如果还没有超出约束,continue
  3. 判断已经输入 word 的个数,如果是1个,添加空格后输入。更新行标记、2中用到的记录变量。
  4. 如果word 为多个,则计算需要填多少个空格,需要填的位置的个数(word个数-1)。
  5. 填写该行的string,然后更新标记。
  6. 遍历结束后判断最后一行的情况。

  计算多个word 时空格的情况,通过记录空格的个数spaceLeft,还有位置个数spaceAddr,(spaceLeft+spaceAddr-1)/spaceAddr,则为最左边的位置空格个数,然后更新 spaceLeft 和 spaceAddr,这样就可以在行添加word 时候把words 计算进去。

 

 

我写的代码:

 1 #include <string>
 2 #include <vector>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 class Solution {
 7 public:
 8     vector<string> fullJustify(vector<string> &words, int L) {
 9         int curLen = 0,startIdx = 0,curWordIdx =-1,wordLen =0;
10         int spaceLeft=0,spaceAddr=0,temp=0;
11         vector<string> ret;
12         string line="";
13         while(++curWordIdx<words.size()){
14             if(curLen+words[curWordIdx].size()<=L){
15                 curLen+=words[curWordIdx].size()+1;
16                 wordLen+= words[curWordIdx].size();
17                 continue;
18             }
19             if(startIdx+1==curWordIdx)
20                 line=words[startIdx]+string(L-words[startIdx].size(),' ');
21             else{
22                 spaceLeft=L - wordLen;
23                 spaceAddr = curWordIdx - startIdx -1;
24                 line = words[startIdx];
25 //                cout<<spaceLeft<<" "<<spaceAddr<<endl;
26                 while(++startIdx<curWordIdx){
27                     temp = (spaceLeft+spaceAddr-1)/spaceAddr;
28                     line+=string(temp,' ');
29                     line+=words[startIdx];
30                     spaceLeft-=temp;
31                     spaceAddr--;
32                 }
33             }
34             ret.push_back(line);
35             spaceLeft = spaceAddr =wordLen= curLen= 0;
36             startIdx = curWordIdx;
37             curWordIdx --;
38             line="";
39         }
40         if(curLen>0){
41 
42             line = words[startIdx];
43             while(++startIdx<curWordIdx){
44                 line+=" "+words[startIdx];
45             }
46             line+=string(L-line.size(),' ');
47             ret.push_back(line);
48         }
49         return ret;
50     }
51 };
52 
53 int main()
54 {
55     vector<string> words={"What","must","be","shall","be."};
56     int l = 12;
57     Solution sol;
58     vector<string> ret = sol.fullJustify(words,l);
59     for(int i =0;i<ret.size();i++)
60         cout<<ret[i]<<"*"<<endl;
61 
62 
63     return 0;
64 }
View Code
 
discuss 中有一个很少行数的实现,大概思路是一样的,写的比我简略,同时处理空格情况比我写的好,过程如下:
 
  1. for遍历输入的words
  2.   for查找k=0,从上一级for下标开始,寻找放入一行的word 的个数,同时记录word 长度和,k 结束为不取值
  3.   for j=0填写如何行
      • 如果i + k>= n,表示这是末尾行,word 之间添加一个空格。
      • 否则,通过 (L-len)/(k-1)+(j<( (L-len)%(k-1) )),为各位置的空格数
  4.       更新行末尾空格(最后一行的情况),将行添加到ret 中。
  5. 结束
 
代码如下:
 
 1 #include <string>
 2 #include <vector>
 3 #include <iostream>
 4 using namespace std;
 5 /**
 6 class Solution {
 7 public:
 8     vector<string> fullJustify(vector<string> &words, int L) {
 9         int curLen = 0,startIdx = 0,curWordIdx =-1,wordLen =0;
10         int spaceLeft=0,spaceAddr=0,temp=0;
11         vector<string> ret;
12         string line="";
13         while(++curWordIdx<words.size()){
14             if(curLen+words[curWordIdx].size()<=L){
15                 curLen+=words[curWordIdx].size()+1;
16                 wordLen+= words[curWordIdx].size();
17                 continue;
18             }
19             if(startIdx+1==curWordIdx)
20                 line=words[startIdx]+string(L-words[startIdx].size(),' ');
21             else{
22                 spaceLeft=L - wordLen;
23                 spaceAddr = curWordIdx - startIdx -1;
24                 line = words[startIdx];
25 //                cout<<spaceLeft<<" "<<spaceAddr<<endl;
26                 while(++startIdx<curWordIdx){
27                     temp = (spaceLeft+spaceAddr-1)/spaceAddr;
28                     line+=string(temp,' ');
29                     line+=words[startIdx];
30                     spaceLeft-=temp;
31                     spaceAddr--;
32                 }
33             }
34             ret.push_back(line);
35             spaceLeft = spaceAddr =wordLen= curLen= 0;
36             startIdx = curWordIdx;
37             curWordIdx --;
38             line="";
39         }
40         if(curLen>0){
41 
42             line = words[startIdx];
43             while(++startIdx<curWordIdx){
44                 line+=" "+words[startIdx];
45             }
46             line+=string(L-line.size(),' ');
47             ret.push_back(line);
48         }
49         return ret;
50     }
51 };
52 */
53 class Solution {
54 public:
55     vector<string> fullJustify(vector<string> &words, int L) {
56         vector<string> ret;
57         for(int i=0,k,len;i<words.size();i+=k){
58             for( k=len=0;i+k<words.size()&&len+words[i+k].size()+k<=L;k++)
59                 len+=words[i+k].size();
60             string temp = words[i];
61             for(int j=0;j<k-1;j++){
62                 if(i+k>=words.size())   temp+=" ";//for the last line.
63                 else    temp+=string((L-len)/(k-1)+(j<( (L-len)%(k-1) )),' ' );
64                 temp+=words[i+j+1];
65             }
66             temp+=string(L-temp.size(),' ');
67             ret.push_back(temp);
68         }
69         return ret;
70     }
71 };
72 
73 int main()
74 {
75     vector<string> words={"What","must","be","shall","be."};
76     int l = 12;
77     Solution sol;
78     vector<string> ret = sol.fullJustify(words,l);
79     for(int i =0;i<ret.size();i++)
80         cout<<ret[i]<<"*"<<endl;
81 
82 
83     return 0;
84 }
View Code

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
posted @ 2014-11-28 15:48  A_zhu  阅读(245)  评论(0编辑  收藏  举报