【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.
【解析】

题意:把一个集合的单词按照每行L个字符放,每行要两端对齐,如果空格不能均匀分布在所有间隔中,那么左边的空格要多于右边的空格,最后一行靠左对齐。

思路:没有什么特别算法,就是模拟,主要分情况判断。首先分两大类,末行和非末行;然后末行所有单词间放一个空格,最后面补充空格;非末行再分两类,如果只有一个单词就靠左放,右边补空格;如果有多个单词,即计算有几个间隔num和几个多余的空格extra(除每两个单词间一个空格外多余的),每个间隔再多方extra/num个,前extra%num个间隔再多放个空格。

【Java代码】

public class Solution {
    public List<String> fullJustify(String[] words, int L) {
        List<String> ans = new ArrayList<String>();
        
        int n = words.length;
        int i = 0;
        while (i < n) {
            int len = words[i].length();
            int j = i + 1;
            while (j < n && len + 1 + words[j].length() <= L) {
                len += 1 + words[j].length();
                j++;
            }
            
            String line = words[i];
            if (j == n) { // if this is the last line
                for (int k = i + 1; k < n; k++) {
                    line += " " + words[k];
                }
                while (line.length() < L) {
                	line += " ";
                }
            } else {
                int extraWhite = L - len;
                int whiteNum = j - i - 1;
                
                if (whiteNum == 0) { // if this line has only one word
                	while (line.length() < L) {
                    	line += " ";
                    }
                } else {
	                for (int k = i + 1; k < j; k++) {
	                    line += " ";
	                    for (int p = 0; p < extraWhite/whiteNum; p++) {
	                        line += " ";
	                    }
	                    if (k - i <= extraWhite%whiteNum) {
	                        line += " ";
	                    }
	                    line += words[k];
	                }
                }
            }
            ans.add(line);
            
            i = j;
        }
        
        return ans;
    }
}


你可能感兴趣的:(LeetCode,String,text,Justification)