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.

答案
这个题应该算是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;
    }
}

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容