[LeetCode] 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.

这道题目说实在挺无聊了。断断续续地刷LeetCode,发现其实里面AC Rate很低的题不一定都很难,有的可能是题意本身比较模糊,各种corner case比较多而已。这题就是典型的思路简单但AC Rate低的题目。


我的思路分为两步,分别用自定义的两个函数pack和convert搞定,感觉这样代码结构会清晰些。

第一步:将words分行,获得每一行的起始词和结尾词的索引;

第二步:将分好行的词群转换为字符串行,主要就是要计算中间插入的空格数。这里需要对于只含单个词的行和最后一行都要进行特殊处理,因为空格分布的规则不一样。


	public ArrayList<String> fullJustify(String[] words, int L) {
		ArrayList<String> ret = new ArrayList<String>();
		if (words == null || words.length == 0)
			return ret;

		int start = 0, end = pack(words, start, L);
		ret.add(convert(words, start, end, L));
		while (end != words.length - 1) {
			start = end + 1;
			end = pack(words, start, L);
			ret.add(convert(words, start, end, L));
		}
		return ret;
	}

	// pack a line and return the end of this line
	private int pack(String[] words, int start, int L) {
		int next = start; // the index of the next word
		int length = words[next].length();

		// always try to include the next word plus a padding space (greedy packing)
		while (next + 1 < words.length
				&& length + 1 + words[next + 1].length() <= L)
			length += words[++next].length() + 1;
		return next;
	}

	// convert multiple words along with extra padding space into a string of length L
	private String convert(String[] words, int start, int end, int L) {
		StringBuilder sb = new StringBuilder();

		// if this line only contains one word
		if (start == end) {
			sb.append(words[start]);
			for (int i = 0; i < L - words[start].length(); i++) {
				sb.append(" ");
			}
			return sb.toString();
		}
		// if the line is the last line, the space distribution rule is different
		else if (end == words.length - 1) {
			int curLen = 0;
			for (int i = start; i < end; i++) {
				sb.append(words[i]);
				sb.append(" ");
				curLen += words[i].length() + 1;
			}
			sb.append(words[end]);
			curLen += words[end].length();

			for (int i = 0; i < L - curLen; i++) {
				sb.append(" ");
			}
			return sb.toString();
		}

		// calculate the lengths of padding space
		int totalLen = 0, numOfSpaces = end - start;
		for (int i = start; i <= end; i++)
			totalLen += words[i].length();
		int lenOfPaddingSpace = (L - totalLen) / numOfSpaces;
		int numOfExtraSpaces = (L - totalLen) % numOfSpaces;

		// construct the line
		int count = 0; // count of the extra spaces
		for (int i = start; i < end; i++) {
			sb.append(words[i]);
			for (int j = 0; j < lenOfPaddingSpace; j++)
				sb.append(" ");
			if (count < numOfExtraSpaces)
				sb.append(" ");
			count++;
		}
		sb.append(words[end]);

		return sb.toString();
	}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值