LeetCode 68 Text Justification(Python詳解及實現)

【題目】

Given an array of words and a length L,format the text such that each line has exactly L characters and is fully (leftand right) justified.

 

You should pack your words in a greedyapproach; 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 bedistributed as evenly as possible. If the number of spaces on a line do notdivide evenly between words, the empty slots on the left will be assigned morespaces than the slots on the right.

 

For the last line of text, it should beleft 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.  "

]

 

輸入一個字符串數組和一個規定長度L,每行包括空格和標點共有L個字符。數組中的每個字符串不能拆開。

 

【思路】

1、以輸出是否爲末行分爲兩類:

l  非末行單詞組

對於非末行單詞組,按照單詞組單詞數量可分爲:

n  單個單詞

只包含一個單詞,規定其左對齊,不足指定長度以空格填充;

n  多個單詞

包含count個單詞,那麼它有(count-1)個間隔,每個間隔放置一個空格;此時,求出不足指定長度需要的額外空格數目space_num,每個單詞間隔填充space_num/(count-1)個空格;若不能整除,那麼前space_num%(count-1)個間隔再次填充一個空格;

l  末行單詞組:

n  只有一個單詞,左對齊,不足指定長度以空格填充;

n  若該組有count個單詞,那麼它有(count-1)個間隔,每個間隔放置一個空格;不足指定長度,末尾填充;

【Python實現】

# -*- coding: utf-8 -*-

"""

Created on Fri Aug  4 09:50:29 2017

 

@author: Administrator

"""

 

class Solution(object):

   def fullJustify(self, words, maxWidth):

       """

       :type words: List[str]

       :type maxWidth: int

        :rtype: List[str]

       """

       res = []

       i = 0

       #print(len(words[0]))

       while i < len(words):

           begin = i #記錄每次統計單詞組的開始位置

           cursize = 0

           row_space = 0

           front_space = 0

           while i < len(words):

                if cursize == 0:

                    newsize = len(words[i])#標爲i單詞長度

                else:

                    newsize = cursize +len(words[i]) + 1

#將下標爲i的單詞加入後該行單詞長度

                if newsize <= maxWidth:

                    cursize = newsize

                else:

                    break

                i = i + 1            

           space_num = maxWidth - cursize#計算該行需要補的空格個數

           if i - begin - 1 > 0 and i < len(words):#i - begin - 1 該行空格個數

                row_space = space_num // (i -begin - 1)

#該行每個單詞之間需要補充的空格數

                front_space = space_num % (i -begin - 1)

#不能剛好整除時,剩餘的空格數量,前front_space個間隔需要再多補充一個空格

           else:

                row_space = 0#不需要補額外空格

           j = begin

            while j < i:

                if j == begin:#若是該行的首個單詞組

                    tmp = words[j]

                else:

                    tmp += ""*(row_space + 1)#補充row_space+1個空格

                    if  front_space > 0 and i < len(words):

#若剩餘的空格數量大於0,改間隔需要再多補一個空格

                        tmp += ""                   

                        front_space -= 1

                    tmp += words[j]

                j += 1

           tmp += " "*front_space

#該組的最後一個單詞,若此時剩餘空格數仍然大於0,此處應該補一個空格

           if len(tmp) != maxWidth:

#針對words最後一個單詞組  words1  word3情況

                tmp += " "*(maxWidth- len(tmp))

           res.append(tmp)                              

       print(res)

       return res

 

 

if __name__ == '__main__':

    S= Solution()

   words =["a","b","c","d","e"]

   #words1 = ["a"]

   #words2 = ["This", "is", "an","example", "of", "text","justification.","This", "is", "an","example", "of", "text","justification."]

   #words3 = [""]

   maxWidth = 3

   S.fullJustify(words, maxWidth)

               

           

               

               

               

                                           

               

            

           

                   

 

發佈了50 篇原創文章 · 獲贊 14 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章