美文网首页
[String]68. Text Justification

[String]68. Text Justification

作者: 野生小熊猫 | 来源:发表于2019-02-09 12:40 被阅读0次
    • 分类:String
    • 时间复杂度: O(n)

    70. Climbing Stairs

    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.

    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 becase 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                  "
    ]
    

    代码:

    class Solution:
        def fullJustify(self, words: 'List[str]', maxWidth: 'int') -> 'List[str]':
            res=[]
            
            if words==None or len(words)==0:
                return res
            
            length=0
            count=0
            
            for i in range(len(words)+1):
                if i==len(words) or length+len(words[i])+count-1>=maxWidth:
                    
                    space=count-1
                    sentence=""
                    if space==0 or i==len(words):
                        #左对齐
                        for j in range(i-count,i-1):
                                sentence+=words[j]+" "
                        sentence+=words[i-1]
                        sentence+=" "*(maxWidth-length-count+1)
                    else:
                        #两边对齐
                        space_length_1=(maxWidth-length)//space
                        space_length_2=(maxWidth-length)%space
                        print(space_length_1,space_length_2)
                        for j in range(i-count,i-1):
                            if space_length_2>0:
                                sentence+=words[j]+" "*(space_length_1+1)
                                space_length_2-=1
                            else:
                                sentence+=words[j]+" "*space_length_1
                        sentence+=words[i-1]       
                    res.append(sentence)  
                    
                    length=0
                    count=0
                    
                if i<len(words):
                    length+=len(words[i])
                    count+=1
                    
            return res
    

    讨论:

    1.这道题不是难,而是麻烦,把corner case都要想清楚:比如全部走完的时候

    相关文章

      网友评论

          本文标题:[String]68. Text Justification

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