美文网首页
Text Justification

Text Justification

作者: BLUE_fdf9 | 来源:发表于2018-08-31 05:21 被阅读0次

题目
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.

答案
这个题应该算是Medium不算hard,因为并不需要构思什么思路。。

class Solution {
    public List<String> fullJustify(String[] words, int maxWidth) {
        List<String> list = new ArrayList<>();
        // curr_strlen: sum of length of all words to be packed
        // curr_word_size: number of words to be packed
        // curr: packed text
        int start = 0, curr_strlen = 0, curr_word_size = 0, base = 0, extra = 0;
        String curr = "";
        for(int i = 0; i <= words.length; i++) {
            // If curr_strlen + words[i].length + curr_word_size can fit into maxWidth, we should do so
            if(i < words.length && curr_strlen + words[i].length() + curr_word_size <= maxWidth) {
                curr_strlen += words[i].length();
                curr_word_size++;
            }
            // If not, stop
            else {
                // Pack words[start] to words[i - 1] into text
                // First, calculate how we want to distribute the empty spaces

                // How many char is left excluding all chars in the words?
                int remain = maxWidth - curr_strlen;
                int slots;
                // How many slots for empty space? depends on how many words
                if(curr_word_size == 1) {
                    slots = 0;
                }
                else {
                    slots = curr_word_size - 1;
                }
                if(slots != 0) {
                    // How many empty space each slot gets if can be divided evenly?
                    base = remain / slots;
                    // How many slots get extra spaces?
                    extra = remain % slots;
                }

                if(i == words.length) {
                    base = 1;
                    extra = 0;
                }
                // Now, pack!
                for(int j = 0; j < curr_word_size; j++) {
                    // First add word
                    curr = curr + words[start + j];
                    if(j < slots) {
                        // Second add base spaces
                        for(int k = 0; k < base; k++) curr = curr + " ";
                        // Third add extra spaces
                        if(j < extra) curr = curr + " ";
                    }
                }
                // Pad spaces behind if necessary
                while(curr.length() < maxWidth)
                    curr = curr + " ";
                // Add curr to list
                list.add(curr);
                curr = "";
                curr_strlen = 0;
                curr_word_size = 0;
                // New start
                start = i;
                if(i >= words.length) break;
                i--;
            }
        }

        return list;
    }
}

相关文章

网友评论

      本文标题:Text Justification

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