leetcode-68-Text Justification

龍仔發表於2019-02-16

“””
68. Text Justification
Description
HintsSubmissionsDiscussSolution
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.

"""
class Solution:
    def justify(self,words_list_in,maxWidth):
        lencur=len(``.join(words_list_in))
        len_spaces=maxWidth-lencur
        index=0
        while len_spaces>0:
            if index>=len(words_list_in)-1:
                index=0
            words_list_in[index]+=` `
            index+=1
            len_spaces-=1
        return ``.join(words_list_in)

    def fullJustify(self, words, maxWidth):
        """
        :type words: List[str]
        :type maxWidth: int
        :rtype: List[str]
        """
        if maxWidth==0:
            return words
        strlist_out=[]
        strdict_cur={`list`:list(),`len`:0}
        index=0
        while True:
            if index>len(words)-1:
                strlist_out.append(strdict_cur[`list`])
                # strdict_cur = {`list`: list(), `len`: 0}
                break
            elif strdict_cur[`len`]<=maxWidth and strdict_cur[`len`]+len(words[index])<=maxWidth:
                strdict_cur[`list`].append(words[index])
                strdict_cur[`len`]+=(len(words[index])+1)
                index+=1
            else:
                strlist_out.append(strdict_cur[`list`])
                strdict_cur = {`list`: list(), `len`: 0}
                # index+=1
        newstr_list=[]
        for words_list in strlist_out:
            new_str=self.justify(words_list,maxWidth)
            newstr_list.append(new_str)
        newstr_list[-1]=` `.join(newstr_list[-1].split())
        space_str=` `*(maxWidth-len(newstr_list[-1]))
        # print([space_str],maxWidth-len(newstr_list[-1]),len(newstr_list[-1]),newstr_list[-1])
        last_str=``.join([newstr_list[-1],space_str])
        # print([last_str])
        newstr_list[-1]=last_str
        # print(newstr_list)
        return newstr_list


if __name__==`__main__`:
    st=Solution()
    words=["This", "is", "an", "example", "of", "text", "justification."]
    L=16
    # words=[""]
    # L=0
    # words=["a"]
    # L=1
    # words=["What","must","be","shall","be."]
    # L=12
    st.fullJustify(words,L)
    """"""