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

 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         int size = words.length;
     4         ArrayList<String> res = new ArrayList<String>();
     5         if(0==size) return res;
     6         int i=0;
     7         while(i<size){
     8             int start = i;
     9             int sum = 0;
    10             while(i<size && sum<=L){
    11                 sum+= words[i].length()+1; // + 1 space
    12                 i++;
    13             }
    14             if(sum-1>L){
    15                 i--; //should come first
    16                 sum-= words[i].length()+1;
    17             }
    18             boolean isLastLine=false;
    19             if(i==size) isLastLine=true;
    20             int end = i-1;
    21             int interval = end-start;
    22             int avgSpace = 0, leftSpace=0;
    23             if(interval>0){
    24                 avgSpace = (L-(sum-interval-1))/interval;
    25                 leftSpace = (L-(sum-interval-1))%interval;
    26             }
    27             StringBuilder sb = new StringBuilder();
    28             for(int j=start;j<end;j++){
    29                 sb.append(words[j]);
    30                 if(isLastLine){
    31                     sb.append(' ');
    32                 }
    33                 else{
    34                     for(int m=0;m<avgSpace;m++){
    35                         sb.append(' ');
    36                     }
    37                     if(leftSpace>0){
    38                         leftSpace--;
    39                         sb.append(' ');
    40                     }
    41                 }
    42             }
    43             sb.append(words[end]);
    44             int length = sb.length();
    45             for(int n=0;n<L-length;n++){
    46                 sb.append(' ');
    47             }
    48             res.add(sb.toString());
    49         }
    50         return res;
    51     }
    52 }
    View Code

     

    if(sum-1>L){
    i--;// 先--
    sum-=words[i].length()+1;
    }

  • 最后只遍历到end-1,不能到end
posted @ 2014-02-10 15:30  krunning  阅读(235)  评论(0编辑  收藏  举报