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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值