美文网首页
68. Text Justification

68. Text Justification

作者: bin_guo | 来源:发表于2018-07-12 12:36 被阅读0次

Leetcode: 68. 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 does 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:
  1. A word is defined as a character sequence consisting of non-space characters only.
  2. Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
  3. The input array words contain 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 because 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 "]

Solution:
class Solution {
    public List<String> fullJustify(String[] words, int maxWidth) {
        List<String> result = new ArrayList<String>();
        int start = 0, end = 1; //end always indexed 1 more than the real end index
        while (start < words.length){
            int spaces = 0;
            int wordsLength = words[start].length();
            while(end < words.length && (spaces + 1 + wordsLength + words[end].length()) <= maxWidth){
                spaces++;
                wordsLength += words[end].length();
                end++;
            }
            if(end == words.length){
                StringBuilder sb = new StringBuilder(words[start]);
                for(int i = start + 1; i < end; i++){
                    sb.append(" " + words[i]);
                }
                for(int i = wordsLength + spaces; i < maxWidth; i++){
                    sb.append(" ");
                }
                result.add(sb.toString());
                break;
            }
            else if((end - start) == 1){
                StringBuilder sb = new StringBuilder(words[start]);
                for(int i = wordsLength; i < maxWidth; i++){
                    sb.append(" ");
                }
                result.add(sb.toString());
            }
            else {
                int spaceEachSpot = (maxWidth - wordsLength) / (end - start -1);
                int restSpaces = maxWidth - wordsLength - (end - start - 1) * spaceEachSpot;
                StringBuilder sb = new StringBuilder(words[start]);
                for(int i = start + 1; i < end; i++){
                    for(int j = 0; j < spaceEachSpot; j++){
                        sb.append(" ");
                    }
                    if(restSpaces-- > 0){
                        sb.append(" ");
                    }
                    sb.append(words[i]);
                }
                result.add(sb.toString());
            }
            start = end++;
        }
        return result;
    }
}

相关文章

网友评论

      本文标题:68. Text Justification

      本文链接:https://www.haomeiwen.com/subject/ioglpftx.html