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

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.

Hide Tags
  String
翻译:

Code:

/**
 * 
 */
package From61;

import java.util.ArrayList;
import java.util.List;

/**
 * @author MohnSnow
 * @time 2015年6月29日 下午5:38:33
 * 
 */
public class LeetCode68 {

	/**
	 * @param argsmengdx
	 *            -fnst
	 */
	public static List<String> fullJustify(String[] words, int maxWidth) {
		List<String> res = new ArrayList<>();
		int count = 0;
		StringBuffer temp = new StringBuffer();
		for (int i = 0; i < words.length; i++) {
			if (words[i].length() < maxWidth - count) {
				temp.append(words[i]);
				temp.append(" ");
				count = count + words[i].length() + 1;
			} else {
				res.add(temp.toString());
				temp.delete(0, temp.capacity());
				temp.append(words[i]);
				temp.append(" ");
				count = 0;
				count = words[i].length() + 1;
			}
		}
		res.add(temp.toString());
		return res;
	}

	//https://leetcode.com/discuss/25949/simple-java-solution
	public static List<String> fullJustify1(String[] words, int L)
	{
		final StringBuilder sb = new StringBuilder();
		for (int i = 0; i < L; ++i)
		{
			sb.append(" ");
		}
		System.out.println("sb: " + sb.toString() + "sb.length():" + sb.length());//为初始字符串加上空格
		final String pads = sb.toString();
		final List<String> strs = new ArrayList<>();
		for (int i = 0, sum = 0, j = 0; i < words.length; i = j)
		{
			for (j = i + 1, sum = words[i].length(); j < words.length && sum + j - i + words[j].length() <= L; ++j)
			{
				sum += words[j].length();
			}
			final StringBuilder l = new StringBuilder();
			final int n = j - 1 - i;
			final int m = (j == words.length || 0 == n) ? 1 : ((L - sum) / n);
			final int b = (j == words.length) ? 0 : (L - sum - m * n);
			for (int k = i; k < j - 1; ++k)
			{
				l.append(words[k]);
				l.append(pads.substring(0, (k - i < b) ? (m + 1) : m));
			}

			l.append(words[j - 1]);
			if (j == words.length || 0 == n)
			{
				l.append(pads.substring(0, L - sum - n));
			}

			strs.add(l.toString());
		}
		return strs;
	}

	public static void main(String[] args) {
		String[] words = { "This", "is", "an", "example", "of", "text", "justification." };
		int maxWidth = 16;
		System.out.println("addBinary: " + fullJustify(words, maxWidth));
		System.out.println("addBinary: " + fullJustify1(words, maxWidth));
	}
}







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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值