`
huntfor
  • 浏览: 195399 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

[leetcode]Text Justification

 
阅读更多

(这道题还是不要看这篇旧博文比较好)新博文地址:[leetcode]Text Justification

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."]
L: 16.

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.

 怎么说呢,这道题不难,但是略烦,毫无疑问的是,该题肯定可以用递归实现。

算法思想:

1. 首先需要从数组中挑出几个可以组成一个String的元素(这有两种情况,是不是最后一行)

2. 如果是最后一行,比较容易,将短缺的长度用空格补充即可

3. 如果不是最后一行,又有几种情况,

3.1. 一个单词组成的行,只需要填充后面

3.2 多个单词组成,但是短缺的长度可以均匀填充

3.3 多个单词组成,但是短缺的长度不可以均匀填充(3.2和3.3一并处理)

4 递归处理数组剩下的部分

代码较长,但是不知道还能怎么优化时间复杂度,当然,可以从代码的简洁度o(╯□╰)o

 

List<String> result = new ArrayList<String>();
	public List<String> fullJustify(String[] words, int L) {
		generateEachLine(words, L);
		return result;
	}
	private void generateEachLine(String[] words, int l) {
		if (words == null || words.length == 0) {
			result.add("");
			return;
		}
		StringBuilder sb = new StringBuilder();
		String[] wordsPerLine = new String[words.length];
		wordsPerLine[0] = words[0];
		int totalLength = wordsPerLine[0].length(), i = 1, spaceCount = 0;
		for (; i < words.length; i++) {
			wordsPerLine[i] = words[i];
			int length = wordsPerLine[i].length() + 1;
			totalLength += length;
			if (totalLength <= l) {// 如果长度不超过l,可以加入该行
				spaceCount++;
			} else {//超过l,就应该放在下一行处理
				totalLength -= length;
				i--;
				break;
			}
		}
		sb.append(wordsPerLine[0]);
		if (i == words.length) {//i== length最后一行
			if (totalLength <= l) {//如果有短缺,用空格补足
				for (int j = 1; j < i; sb.append(" ").append(wordsPerLine[j]), j++);
				for (int j = 0; j < l - totalLength; sb.append(' '), j++);
				result.add(sb.toString());
				return;
			}
		} else {// 不是最后一行
			if (spaceCount == 0) {//如果只有一个单词
				for (int j = 0; j < l - totalLength; sb.append(' '), j++);
			} else {
				int spaceNeed2Add = l - totalLength;
				int spaceLeft = spaceNeed2Add % spaceCount;
				int spaceAverge = spaceNeed2Add / spaceCount;
				for (int j = 1; j <= i; j++) {//这里将3.2 3.3一起处理了
					for (int k = 0; k < spaceAverge; k++, sb.append(' '));
					if (spaceLeft > 0) {
						sb.append(' ');
					}
					spaceLeft--;
					sb.append(' ').append(wordsPerLine[j]);
				}
			}
			result.add(sb.toString());
			String[] left = new String[words.length - i - 1];//这里很别扭,不知道咋优化
			for (int j = i + 1; j < words.length; left[j - i - 1] = words[j], j++);
			generateEachLine(left, l);
		}
	}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics