首页 > 其他 > 详细

LeetCode | Text Justification

时间:2014-03-14 00:41:40      阅读:563      评论:0      收藏:0      [点我收藏+]

题目

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.
分析

按照题目要求完成即可,注意corner cases

代码

import java.util.ArrayList;

public class TextJustification {
	public ArrayList<String> fullJustify(String[] words, int L) {
		ArrayList<String> results = new ArrayList<String>();

		int wordsLen = 0;
		int head = 0;
		for (int i = 0; i < words.length; ++i) {
			if (head == i) {
				if (i == words.length - 1) {
					StringBuilder lineBuilder = new StringBuilder();
					lineBuilder.append(words[i]);
					int remain = L - lineBuilder.length();
					for (int k = 0; k < remain; ++k) {
						lineBuilder.append(" ");
					}
					results.add(lineBuilder.toString());
				} else {
					wordsLen += words[i].length();
				}
			} else {
				if (wordsLen + words[i].length() + 1 > L) {
					StringBuilder lineBuilder = new StringBuilder();
					int intervalNum = i - head - 1;
					if (intervalNum == 0) {
						// corner case
						lineBuilder.append(words[i - 1]);
						int remain = L - lineBuilder.length();
						for (int k = 0; k < remain; ++k) {
							lineBuilder.append(" ");
						}
						results.add(lineBuilder.toString());
					} else {
						int totalSpaceNum = L - (wordsLen - intervalNum);
						int redundantSpaceNum = totalSpaceNum % intervalNum;
						int spaceNumPerInterval = totalSpaceNum / intervalNum;
						for (int j = head; j < i - 1; ++j) {
							lineBuilder.append(words[j]);
							for (int k = 0; k < spaceNumPerInterval; ++k) {
								lineBuilder.append(" ");
							}
							if (j - head < redundantSpaceNum) {
								lineBuilder.append(" ");
							}
						}
						lineBuilder.append(words[i - 1]);
						results.add(lineBuilder.toString());
					}
					// new line
					head = i;
					wordsLen = 0;
					--i;
				} else if (wordsLen + words[i].length() + 1 == L) {
					StringBuilder lineBuilder = new StringBuilder();
					for (int j = head; j <= i; ++j) {
						lineBuilder.append(words[j]).append(" ");
					}
					results.add(lineBuilder.substring(0,
							lineBuilder.length() - 1));

					// new line
					head = i + 1;
					wordsLen = 0;
				} else {
					if (i == words.length - 1) {
						StringBuilder lineBuilder = new StringBuilder();
						for (int j = head; j <= i; ++j) {
							lineBuilder.append(words[j]).append(" ");
						}
						int remain = L - lineBuilder.length();
						for (int k = 0; k < remain; ++k) {
							lineBuilder.append(" ");
						}
						results.add(lineBuilder.toString());
					} else {
						wordsLen += words[i].length() + 1;
					}
				}
			}
		}
		return results;
	}
}

LeetCode | Text Justification,布布扣,bubuko.com

LeetCode | Text Justification

原文:http://blog.csdn.net/perfect8886/article/details/21190003

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!