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

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.

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 public class Solution {
 2     public ArrayList<String> fullJustify(String[] words, int L) {
 3         // Start typing your Java solution below
 4         // DO NOT write main() function
 5         if(words == null || words.length == 0) return null;
 6         StringBuffer al = new StringBuffer();
 7         ArrayList<String> result = new ArrayList<String>();
 8         int cur = 0;
 9         int ini = 0;
10         int end = 0;
11         for(int i = 0; i < words.length; i ++){
12             if(words[i].length() + cur <= L){
13                 al.append(words[i]);
14                 cur += words[i].length();
15             }else{
16                 end = i - 1;
17                 int rest = L - cur;
18                 if(al.length() > 1){
19                     int other = rest / (al.length() - 1);
20                     for(int ii = end; ii > ini + 1; ii --){
21                         for(int j = 0; j < other; j ++){
22                             cur = cur - words[ii].length();
23                         al.insert(cur," ");
24                         }
25                     }
26                     for(int j = 0; j < rest - other * (al.length() - 2); j ++){
27                         
28                         al.insert(words[ini].length()," ");
29                     }
30                 }else{
31                     for(int j = 0; j < rest; j ++){
32                         al.append(" ");
33                     }
34                 }
35                 result.add(al.toString());
36                 ini = i;
37                 al = new StringBuffer();
38                 al.append(words[i]);
39                 cur = words[i].length();
40             }
41         } 
42         int rest = L - cur;
43         if(al.length() > 1){
44             int other = rest / (al.length() - 1);
45             for(int ii = end; ii > ini + 1; ii --){
46                 for(int j = 0; j < other; j ++){
47                     cur = cur - words[ii].length();
48                 al.insert(cur," ");
49                 }
50             }
51             for(int j = 0; j < rest - other * (al.length() - 2); j ++){
52                 
53                 al.insert(words[ini].length()," ");
54             }
55         }else{
56             for(int j = 0; j < rest; j ++){
57                 al.append(" ");
58             }
59         }
60         result.add(al.toString());
61         return result;
62     }
63 }

 

网上的解法, 思路是一样的。

 1 public class Solution {
 2     public ArrayList<String> fullJustify(String[] words, int L) {
 3         // Start typing your Java solution below
 4         // DO NOT write main() function
 5         int n = words.length, start=0;
 6         ArrayList<String> res = new ArrayList<String>();
 7 
 8         while(start<n){  
 9             int cur=start, 
10                 count=words[cur].length();
11             StringBuilder s = new StringBuilder(words[cur]);
12             while(count<=L && cur<n){
13                 cur++;
14                 if(cur<n) count+=1+words[cur].length();
15             }
16         
17             if(cur<n){
18                 count-=1+words[cur].length();
19                 cur--;
20                 if(cur>start){
21                 int a = (L-count+cur-start)/(cur-start),
22                     b = (L-count+cur-start)%(cur-start);
23                 for(int i=start+1;i<start+1+b;i++){
24                     for(int j=0;j<a+1;j++)
25                         s.append(" ");
26                     s.append(words[i]);
27                 }
28                 for(int i=start+1+b;i<=cur;i++){
29                     for(int j=0;j<a;j++)
30                         s.append(" ");
31                     s.append(words[i]);
32                 }}else{
33                     for(int i=0;i<L-count;i++)
34                      s.append(" ");
35                 } 
36             }else{
37                 
38                 for(int i=start+1;i<cur;i++){
39                     s.append(" ");
40                     s.append(words[i]);
41                 } 
42                 for(int i=0;i<L-count;i++)
43                      s.append(" ");
44             }     
45             start = cur+1;           
46             res.add(s.toString());
47         }
48         return res;
49         
50     }
51 }

 

posted on 2013-10-02 05:22  Step-BY-Step  阅读(212)  评论(0编辑  收藏  举报

导航