LeetCode 68 questions: about text alignment (difficult)

LeetCode 68 questions: about text alignment (difficult)

  • Title: Given an array and a word length maxWidth, reformat words, making each row has exactly maxWidth characters, and the left and right justified text. It requires a uniform distribution of the number of spaces between words as possible. If the spaces between words can not be evenly distributed in a row, the number of spaces placed left to the right than the number of spaces. The last line of text should be left justified, and not insert extra spaces between words. (Note:
    the word refers to a sequence of characters by a length of non-space characters of each word is larger than 0, less than or equal maxWidth input array of words comprises words at least one word...)
    Here Insert Picture Description

  • An idea: the first word and determining a length of at least one space, and then count the number of the need to add extra space sequentially added to the word and a space character string.

class Solution {
    public List<String> fullJustify(String[] words, int maxWidth) {
        //答案链表
        List<String> ans = new ArrayList<String>();
        //链表中的每个字符串
        String ss = "";
        int index = 0;
        int len = 0;
        for(int i=0;i<words.length;){
            //记录每个字符串的起始单词的数组下标
            index = i;
            //字符串当前长度
            len = words[i++].length();
            //字符串长度小于规定长度
            while(i<words.length && len+words[i].length()<maxWidth){
                //字符串长度=单词长度+至少一个空格长度
                len+=words[i].length()+1;
                i++;
            }
            //最后一个单词
            if(i==words.length){
                ss+=words[index++];
                while(index<i){
                    ss+=" "+words[index++];
                }
                while(len<maxWidth){
                    ss+=" ";
                    len++;
                }
            }
            //只有一个单词
            if(i-index==1){
                ss+=words[index];
                while(len<maxWidth){
                    ss+=" ";
                    len++;
                }
            }
            //正常情况
            if(index<i-1 && len<=maxWidth){
                //需要增加空格的数量
                int len1 = maxWidth-len;
                //均匀分布的空格数量+至少一个空格
                int num1 = len1/(i-index-1)+1;
                //无法均匀分布的空格
                int num2 = len1%(i-index-1);
                //字符串中加入第一个单词
                ss+=words[index++];
                while(index<i){
                    for(int j=0;j<num1;j++){
                        ss+=" ";
                    }
                    if(num2!=0){
                        ss+=" ";
                        num2--;
                    }
                    ss+=words[index++];
                }
            }
            ans.add(ss);
            ss = "";
        }
        return ans;
    }
}

Here Insert Picture Description

Published 79 original articles · won praise 7 · views 1371

Guess you like

Origin blog.csdn.net/new_whiter/article/details/104376037
Recommended