码迷,mamicode.com
首页 > 其他好文 > 详细

Text Justification

时间:2014-07-06 16:10:10      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   strong   os   art   

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

Return the formatted lines as:

[
   "This    is    an",
   "example  of text",
   "justification.  "
]
总结:自然思路,主要是注意边界条件。
思路:计算一行最多能承载几个单词,然后处理这一行通过按要求加空格。
注意事项:corner case:
[""],0
[""],2
["a"],2
还有就是空格不能均匀分布时,左边多。

 1 class Solution {
 2 public:
 3     vector<string> fullJustify(vector<string> &words, int L) {
 4         vector<string> res;
 5         vector<int> word_len;
 6         int len=words.size();
 7         for(int i=0;i<len;i++)
 8         word_len.push_back(words[i].size());
 9         
10         int len_tot=0;
11         int start=0;
12         int count=0;
13         int i=0;
14         for(;i<word_len.size();)
15         {
16             count=i-start+1;
17             if(len_tot+word_len[i]+count-2>=L)
18             {
19                 process(words,start, i-1,len_tot,L,res);
20                 len_tot=0;
21                 start=i;
22             }
23             else
24             {
25                 len_tot+=word_len[i];
26                 i++;
27             }
28         }
29         if(len_tot>=0&&len_tot<=L)//last line
30         {
31             process(words,start,i-1,len_tot,L,res);
32         }
33         return res;
34     }
35     void process(vector<string> &words, int start, int end, int len_tol, int L, vector<string> &res)
36     {
37         int count=end-start+1; 
38         string s="";
39         string space;
40         string end_space;
41         int remain=0;
42         if(end+1==words.size()||count==1)//last line
43         {
44         int extra=L-len_tol-(count-1);
45         space= ;
46         end_space=string(extra, );
47         }
48         else
49         {
50         int extra=(L-len_tol)/(count-1);
51         remain=(L-len_tol)%(count-1);
52         
53         space=string(extra,  );
54         }
55         while(start<end)
56         {
57             if(remain>0)
58             {
59             s+=words[start++]+space+ ;
60             remain--;
61             }else s+=words[start++]+space;
62         }
63         s=s+words[end]+end_space;//last word
64         
65         res.push_back(s);
66     }
67 };

 

Text Justification,布布扣,bubuko.com

Text Justification

标签:style   blog   color   strong   os   art   

原文地址:http://www.cnblogs.com/hicandyman/p/3825370.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!