[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();
	}


發佈了60 篇原創文章 · 獲贊 8 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章