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)

               

           

               

               

               

                                           

               

            

           

                   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值