15.14 Text Justification

Link: https://oj.leetcode.com/problems/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 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.  "
]

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

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.
My thought: This Q needs two steps. First find out how many words can be put in one line. The other is how to add spaces between words. I feel both steps difficult.

The code below comes from: http://www.darrensunny.me/leetcode-text-justification/

package me.darrensunny.leetcode;

import java.util.ArrayList;

/**
 * LeetCode - Text Justification
 * Created by Darren on 14-5-11.
 */
public class TextJustification {

    // 532ms for 24 test cases
    public ArrayList<String> fullJustify(String[] words, int L) {
        ArrayList<String> result = new ArrayList<String>();
        if (words == null || words.length == 0)
            return result;
        int begin = 0, end = 0;     // words[begin...end-1] as a line
        while (begin < words.length) {
            // Determine end such that words[begin...end-1] fit in a line and
            // words[begin...end] do not.
            int currentLength = words[begin].length();
            for (end = begin+1; end < words.length; end++) {
                if (currentLength + words[end].length() + 1 <= L)
                    currentLength += words[end].length() + 1;
                else
                    break;
            }
            // Construct a justified line with words[begin...end-1]
            StringBuilder temp = new StringBuilder();
            temp.append(words[begin]);
            if (end == words.length || end == begin+1) {    // Last line or a line with only one word
                // Left justified
                for (int i = begin+1; i < end; i++) {//starts with begin+1 since begin is saved into temp
                    temp.append(' ');
                    temp.append(words[i]);
                }
                for (int i = 0; i < L - currentLength; i++)
                    temp.append(' ');
            } else {// Regular lines
                // Fully justified
                int spaceInBetween = end - begin - 1;//#spaces = #words-1
                double spaces = L - currentLength + spaceInBetween;
                for (int i = begin+1; i < end; i++) {
                    for (int j = 0; j < spaces/spaceInBetween; j++) {
                        temp.append(' ');
                    }
                    spaces -= Math.ceil(spaces/spaceInBetween);
                    spaceInBetween--;
                    temp.append(words[i]);
                }
            }
            // Add the line to the resulting list, and slide the window to the next position
            result.add(temp.toString());
            begin = end;
        }
        return result;
    }
}
Note: to do again

發佈了153 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章