LeetCode 68. Text Justification
原题链接在这里:https://leetcode.com/problems/text-justification/
题目:
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth 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 maxWidth 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.
Note:
- A word is defined as a character sequence consisting of non-space characters only.
- Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
- The input array
words
contains at least one word.
Example 1:
Input: words = ["This", "is", "an", "example", "of", "text", "justification."] maxWidth = 16 Output: [ "This is an", "example of text", "justification. " ]
Example 2:
Input: words = ["What","must","be","acknowledgment","shall","be"] maxWidth = 16 Output: [ "What must be", "acknowledgment ", "shall be " ] Explanation: Note that the last line is "shall be " instead of "shall be", because the last line must be left-justified instead of fully-justified. Note that the second line is also left-justified becase it contains only one word.
Example 3:
Input: words = ["Science","is","what","we","understand","well","enough","to","explain", "to","a","computer.","Art","is","everything","else","we","do"] maxWidth = 20 Output: [ "Science is what we", "understand well", "enough to explain to", "a computer. Art is", "everything else we", "do " ]
题解:
首先计算本行能fit进几个词. 剩余的空间用空格填补. 计算两个词之间应该有多少个空格.
特例是一行只有一个词或者最后一行.
Time Complexity: O(n). n = words.length. 每个词不会被扫两遍.
Space: O(maxWidth). regardless res.
AC Java:
1 class Solution { 2 public List<String> fullJustify(String[] words, int maxWidth) { 3 List<String> res = new ArrayList<>(); 4 if(words == null || words.length == 0){ 5 return res; 6 } 7 8 int n = words.length; 9 int cur = 0; 10 for(int i = 0; i < n; i = cur){ 11 // len初始值设成-1是为了抵消掉本行最后一个词后面的空格 12 int len = -1; 13 for(cur = i; cur < n && len + words[cur].length() + 1 <= maxWidth; cur++){ 14 len += words[cur].length() + 1; 15 } 16 17 // For the last line, we still need to append 1 space between words. 18 int space = 1; 19 int extra = 0; 20 21 // 本行不单单只有一个单词,并且这不是最后一行 22 if(cur != i + 1 && cur != n){ 23 // cur已经跳到了下一行的开始次, 本行共有cur-i-1个间隙 24 // 最后再加一是因为本身词与词之间就有一个空格 25 space = (maxWidth - len) / (cur - i - 1) + 1; 26 extra = (maxWidth - len) % (cur - i - 1); 27 } 28 29 StringBuilder sb = new StringBuilder(words[i]); 30 for(int j = i + 1; j < cur; j++){ 31 for(int s = 0; s < space; s++){ 32 sb.append(' '); 33 } 34 35 if(extra-- > 0){ 36 sb.append(' '); 37 } 38 39 sb.append(words[j]); 40 } 41 42 // 本行只有一个单词,或者这是最后一行, 直接靠左, 后面用空格填满. 43 int count = maxWidth - sb.length(); 44 while(count-- > 0){ 45 sb.append(' '); 46 } 47 48 res.add(sb.toString()); 49 } 50 51 return res; 52 } 53 }