leetcode 68:Text Justification细致分析,java实现

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

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.

题目分析:要求把字符串按照每一行平衡整理。简单的写写,考虑多种情况。

1.第一种:如果是最后一行,则从左到右依次排序。剩下的填充空格。

2.第二种:如果一行只有一个,则靠左,剩下的填充空格。

3. 第三种:正常行,正常个个数。首先,我们需要知道我们可以填充的空格总数spaces,等于L-单词站位总数,之后每一个间隔num可以填充的为spaces/num,前spaces/num可以多添加一个空格。

public class TextJustification {
	public List<String> fullJustify(String[] words, int maxWidth) {
		List<String> res = new ArrayList<String>();
		int len = words.length;
		if (len < 1)
			return res;
		int i = 0;
		while (i < len) {
			int size = 0;
			size += words[i].length();
			int j = i + 1;
			while (j < len && size + 1 + words[j].length() <= maxWidth) {
				size = size + 1 + words[j].length();
				j++;
			}
			String line = words[i];
			//第一种:如果是最后一行,则从左到右依次排序。剩下的填充空格。
			if (j == len) {
				for (int k = i + 1; k < j; k++) {
					line += " " + words[k];
				}
				while (line.length() < maxWidth) {
					line += " ";
				}
				res.add(line);
			} else {
				//第二种:如果一行只有一个,则靠左,剩下的填充空格。
				if (j - i == 1) {
					while (line.length() < maxWidth) {
						line += " ";
					}
					res.add(line);
				} else {
					/*第三种:正常行,正常个个数。首先,我们需要知道我们可以填充的空格总数spaces,
					等于L-单词站位总数,之后每一个间隔num可以填充的为spaces/num,前spaces/num可以多添加一个空格。*/
					int num = j - i - 1;//间隔数
					int actuallen = size - num;//单词实际站位数
					int spaces = maxWidth - actuallen;//可填充的总得空格数
					int x = spaces / num;
					int y = spaces % num;
					int count = 1;
					for (int k = i + 1; k < j; k++) {
						for (int m = 0; m < x; m++) {
							line += " ";
						}
						if (count <= y) {
							line += " ";
						}
						count++;
						line += words[k];
					}
					res.add(line);
				}
			}
			i = j;
		}
		return res;
	}

	@Test
	public void case1() {
		String[] s = { "This", "is", "an", "example", "of", "text",
				"justification." };
		String[] res = { "This    is    an", "example  of text",
				"justification.  " };
		List<String> expected = new ArrayList<String>();
		Collections.addAll(expected, res);
		List<String> actual = fullJustify(s, 16);
		for (int i = 0; i < expected.size(); i++) {
			String a = expected.get(i);
			String b = actual.get(i);
			Assert.assertEquals(a, b);
		}
	}

	@Test
	public void case2() {
		String[] s = { "What", "must", "be", "shall", "be." };
		String[] res = { "What must be", "shall be.   " };
		List<String> expected = new ArrayList<String>();
		Collections.addAll(expected, res);
		List<String> actual = fullJustify(s, 12);
		for (int i = 0; i < expected.size(); i++) {
			String a = expected.get(i);
			String b = actual.get(i);
			Assert.assertEquals(a, b);
		}
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值