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 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."]
L:16.

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.
import java.util.*;
 
public class Solution {
    public ArrayList<String> fullJustify(String[] words, int maxWidth) {
        ArrayList<String> res = new ArrayList<String>();
  
        int n = words.length;
        int i = 0;
  
        while (i < n) {
            StringBuilder sb = new StringBuilder();
            int last = i + 1;
            int len = words[i].length();
            while (last < n && len + 1 + words[last].length() <= maxWidth) {
                len += 1 + words[last].length();
                last++;
            }
            // 最后一行
            if (last == n) {
                for (int j = i; j < n; j++) {
                    sb.append(words[j] + " ");
                }
                sb.deleteCharAt(sb.length() - 1);
                for (int j = sb.length(); j < maxWidth; j++) {
                    sb.append(" ");
                }
            } else {
                // 只有一个word
                if (last - i == 1) {
                    sb.append(words[i]);
                    for (int j = words[i].length(); j < maxWidth; j++)
                        sb.append(" ");
                } else {// 有多个单词
                    int wordNum = last - i;
                    int wordTotal = 0;
                    for (int j = i; j < last; j++) {
                        wordTotal += words[j].length();
                    }
                    // eachSpace为每个单词间的空格数;r是余数,表示前r个空格数为eachSpace+1
                    int eachSpace = (maxWidth - wordTotal) / (wordNum - 1);
                    int r = (maxWidth - wordTotal) % (wordNum - 1);
  
                    for (int j = i; j < last; j++) {
                        sb.append(words[j]);
                        if (j < last - 1) {
                            for (int k = 0; k < eachSpace + ((j - i) < r ? 1 : 0); k++) {
                                sb.append(" ");
                            }
                        }
                    }
                }
            }
            res.add(sb.toString());
            i = last;
        }
        return res;
    }
      
}

猜你喜欢

转载自blog.csdn.net/weixin_42146769/article/details/89061310