首页 > 代码库 > [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."]
L16.

Return the formatted lines as:

[   "This    is    an",   "example  of text",   "justification.  "]

 

Note: Each word is guaranteed not to exceed L in length.

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.

这道题。。。毫无算法可言啊,非说有的话,那就只能说是最简单的递归了。

但是这道题很恶心,很繁琐,可以说是考察思维是否缜密。一遍通过还是比较困难的。

第一遍:

Input:["What","must","be","shall","be."],

Output:   ["What must be","shall    be."]

Expected:["What must be","shall be.   "]

木有看清题意:the last line of text, it should be left justified and no extra space is inserted between words.

第二遍:

Line 44: java.lang.ArithmeticException: / by zero
["Listen","to","many,","speak","to","a","few."], 6

没有考虑边界情况:A line other than the last line might contain only one word. What should you do in this case?

这个代码应该是我第二遍,到目前为止,最不满意的一个了,可读性真心不敢恭维:

 1 public class Solution { 2     List<String> res = new ArrayList<String>(); 3     public List<String> fullJustify(String[] words, int L) { 4         dfs(words, L); 5         return res; 6     } 7     private void dfs(String words[], int l) { 8         if (words.length == 0)    return; 9         int currentLength = words[0].length(), spaceCount = 0;10         StringBuilder sb = new StringBuilder(words[0]);11         for (int i = 0; i < words.length; i++) {12             if (i > 0)    currentLength += words[i].length() + 1;13             if (currentLength <= l) {14                 if (i == words.length - 1) {15                     spaceCount = Math.max(i, 0);16                     fillSpace(sb, words, currentLength, spaceCount, l, true);17                     return;18                 } else    continue;19             } else {20                 currentLength -= words[i].length() + 1;21                 spaceCount = i - 1;22                 fillSpace(sb, words, currentLength, spaceCount, l, false);23                 String[] left = Arrays.copyOfRange(words, i, words.length);24                 dfs(left, l);25                 break;26             }27         }28     }29     private void fillSpace(StringBuilder sb, String[] words, int currentLength,30             int spaceCount, int l, boolean lastLine) {31         StringBuilder[] spaces = new StringBuilder[spaceCount];32         if (lastLine) {33             for (int i = 1; i < words.length; i++) {34                 sb.append(‘ ‘).append(words[i]);35             }36             while (sb.length() < l)    sb.append(‘ ‘);37             res.add(sb.toString());38             return;39         }40         if (spaceCount == 0) {41             for (int i = 0; i < l - currentLength; sb.append(‘ ‘), i++);42             res.add(sb.toString());43             return;44         }45         for (int j = 0; j < spaceCount; spaces[j++] = new StringBuilder(" "));46         int index = 0;47         for (int j = 0; j < l - currentLength; j++) {48             spaces[index++ % (spaceCount)].append(‘ ‘);49         }50         for (int j = 0; j < spaceCount; j++) {51             sb.append(spaces[j]);52             if (j + 1 < words.length)53                 sb.append(words[j + 1]);54         }55         res.add(sb.toString());56     }57     58 }