| 
 | 1 | +# [Text Justification][title]  | 
 | 2 | + | 
 | 3 | +## Description  | 
 | 4 | + | 
 | 5 | +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.  | 
 | 6 | + | 
 | 7 | +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.  | 
 | 8 | + | 
 | 9 | +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.  | 
 | 10 | + | 
 | 11 | +For the last line of text, it should be left justified and no extra space is inserted between words.  | 
 | 12 | + | 
 | 13 | +For example,  | 
 | 14 | +**words**: `["This", "is", "an", "example", "of", "text", "justification."]`  | 
 | 15 | +**L**: `16`.  | 
 | 16 | + | 
 | 17 | +Return the formatted lines as:  | 
 | 18 | + | 
 | 19 | +```  | 
 | 20 | +[  | 
 | 21 | + "This is an",  | 
 | 22 | + "example of text",  | 
 | 23 | + "justification. "  | 
 | 24 | +]  | 
 | 25 | +
  | 
 | 26 | +```  | 
 | 27 | + | 
 | 28 | +**Note:** Each word is guaranteed not to exceed *L* in length.  | 
 | 29 | + | 
 | 30 | +**Corner Cases:**  | 
 | 31 | + | 
 | 32 | +- A line other than the last line might contain only one word. What should you do in this case?  | 
 | 33 | + In this case, that line should be left-justified.  | 
 | 34 | + | 
 | 35 | +**Tags:** String  | 
 | 36 | + | 
 | 37 | + | 
 | 38 | +## 思路  | 
 | 39 | + | 
 | 40 | +题意是给你一组单词和最大行宽,让你对齐他们,对齐的规则就是尽可能一行可以放下足够多的单词,如果最后有多余的空格,那就把空格均匀地插入到单词之间,如果不能平分的话,那就从左开始依次多插一个空格,最后一行单词之间就正常地一个空格即可,如果凑不到最大行宽,那就在末尾补充空格即可,描述地比较差,不懂的话其实看看demo也就懂了哈。题还是比较坑的,毕竟踩的比赞的人多,我也是靠模拟老老实实做出来的,求出可以最多插入空格数,然后用它除以可以插入的槽数获取每个单词之间的空格,它两取余的话就是最面需要多插入一个空格的个数,最后一行的话就单独处理即可。  | 
 | 41 | + | 
 | 42 | +```java  | 
 | 43 | +class Solution {  | 
 | 44 | + public List<String> fullJustify(String[] words, int maxWidth) {  | 
 | 45 | + int len = words.length;  | 
 | 46 | + List<String> ans = new ArrayList<>();  | 
 | 47 | + StringBuilder spaces = new StringBuilder();  | 
 | 48 | + for (int i = 0; i < maxWidth; ++i) {  | 
 | 49 | + spaces.append(" ");  | 
 | 50 | + }  | 
 | 51 | + int curLen = -1, start = 0;  | 
 | 52 | + for (int i = 0; i < len; ++i) {  | 
 | 53 | + if (curLen + words[i].length() + 1 <= maxWidth) {  | 
 | 54 | + curLen += words[i].length() + 1;  | 
 | 55 | + } else {  | 
 | 56 | + StringBuilder sub = new StringBuilder(words[start]);  | 
 | 57 | + int rest = maxWidth - curLen;  | 
 | 58 | + int l = i - start - 1;  | 
 | 59 | + if (l <= 0) {  | 
 | 60 | + sub.append(spaces.substring(0, rest));  | 
 | 61 | + } else {  | 
 | 62 | + int m = rest / l + 1;  | 
 | 63 | + int mod = rest % l;  | 
 | 64 | + for (int j = start + 1; j < i; ++j) {  | 
 | 65 | + if (mod-- > 0) {  | 
 | 66 | + sub.append(spaces.substring(0, m + 1)).append(words[j]);  | 
 | 67 | + } else {  | 
 | 68 | + sub.append(spaces.substring(0, m)).append(words[j]);  | 
 | 69 | + }  | 
 | 70 | + }  | 
 | 71 | + }  | 
 | 72 | + ans.add(sub.toString());  | 
 | 73 | + start = i;  | 
 | 74 | + curLen = words[i].length();  | 
 | 75 | + }  | 
 | 76 | + }  | 
 | 77 | + StringBuilder sub = new StringBuilder(words[start]);  | 
 | 78 | + for (int i = start + 1; i < len; ++i) {  | 
 | 79 | + sub.append(" ").append(words[i]);  | 
 | 80 | + }  | 
 | 81 | + ans.add(sub + spaces.substring(0, maxWidth - sub.length()));  | 
 | 82 | + return ans;  | 
 | 83 | + }  | 
 | 84 | +}  | 
 | 85 | +```  | 
 | 86 | + | 
 | 87 | + | 
 | 88 | +## 结语  | 
 | 89 | + | 
 | 90 | +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]  | 
 | 91 | + | 
 | 92 | + | 
 | 93 | + | 
 | 94 | +[title]: https://leetcode.com/problems/text-justification  | 
 | 95 | +[ajl]: https://github.com/Blankj/awesome-java-leetcode  | 
0 commit comments