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

 

Note: Each word is guaranteed not to exceed L in length.

Ref: http://fisherlei.blogspot.com/2013/01/leetcode-text-justification.html
 
 
public class Solution {
    public ArrayList<String> fullJustify(String[] words, int L) {
        int wordsCount = words.length;
        ArrayList<String> result = new ArrayList<String>();
        int curLen = 0;
        int lastI = 0;
        for (int i = 0; i <= wordsCount; i++) {
           // 如果是最后一个单词 或 现有长度加上下一个单词和每个单词至少空一个格的长度和大于 L, 就不加下一个单词了,将现有单词组合成string
            if (i == wordsCount || curLen + words[i].length() + i - lastI > L) {
                StringBuffer buf = new StringBuffer();
                  // 计算需要多少个空格
                int spaceCount = L - curLen;
                //计算要把空格分成几份,也就是这一行的单词个数-1
                int spaceSlots = i - lastI - 1;
                if (spaceSlots == 0 || i == wordsCount) {
                    for(int j = lastI; j < i; j++){
                        buf.append(words[j]);
                        if(j != i - 1)
                            appendSpace(buf, 1);
                    }
                    appendSpace(buf, L - buf.length());
                } else {
                    // 两个单词之间要填多少个空格
                    int spaceEach = spaceCount / spaceSlots;
                    // 剩余的空格的数量
                    int spaceExtra = spaceCount % spaceSlots;
                    for (int j = lastI; j < i; j++) {
                        buf.append(words[j]);
                        // j 不是这一行的最后一个数
                        if (j != i - 1) 
                        //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
                            appendSpace(buf, spaceEach + (j - lastI < spaceExtra ? 1 : 0));
                    }
                }
                result.add(buf.toString());
                lastI = i; // 上一行遍历到的那个单词的下标(注意此单词还未添加到result中)
                curLen = 0;
            }
            if (i < wordsCount)
                curLen += words[i].length();
        }
        return result;
    }

    private void appendSpace(StringBuffer sb, int count) {
        for (int i = 0; i < count; i++)
            sb.append(' ');
    }
}

 

 

posted @ 2014-02-12 13:58  Razer.Lu  阅读(250)  评论(0编辑  收藏  举报