博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Leetcode]@python 68. Text Justification
阅读量:5068 次
发布时间:2019-06-12

本文共 2362 字,大约阅读时间需要 7 分钟。

题目链接

题目原文

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

题目大意

输入一个字符串数组和一个规定长度maxWidth。将这个字符串数组的元素尽可能放到长度的L的字符串中,数组中的字符串不能拆开,一个长度L的字符串包括的若干个字符串之间用相等的空格间隔开;如果空格数不能平均分配,则规定左边的空格数可以大于右边的空格数

解题思路

直接模拟解决即可,但需要注意细节的处理,例如从开始加入元素,长度增加,然后空格+1后继续加入新元素,直到长度大于maxWidth

代码

class Solution(object):    def fullJustify(self, words, maxWidth):        """        :type words: List[str]        :type maxWidth: int        :rtype: List[str]        """        ans = []        i = 0        while i < len(words):            size = 0            begin = i            while i < len(words):                if size == 0:                    newsize = len(words[i])                else:                    newsize = size + len(words[i]) + 1                if newsize <= maxWidth:                    size = newsize                else:                    break                i += 1            spaceCnt = maxWidth - size            if i - begin - 1 > 0 and i < len(words):                everyCount = spaceCnt // (i - begin - 1)                spaceCnt %= i - begin - 1            else:                everyCount = 0            j = begin;s=""            while j < i:                if j == begin:                    s = words[j]                else:                    s += ' ' * (everyCount + 1)                    if spaceCnt > 0 and i < len(words):                        s += ' '                        spaceCnt -= 1                    s += words[j]                j += 1            s += ' ' * spaceCnt            ans.append(s)        return ans

转载于:https://www.cnblogs.com/slurm/p/5124905.html

你可能感兴趣的文章
django Models 常用的字段和参数
查看>>
IOS--沙盒机制
查看>>
使用 JointCode.Shuttle 访问任意 AppDomain 的服务
查看>>
sqlite的坑
查看>>
digitalocean --- How To Install Apache Tomcat 8 on Ubuntu 16.04
查看>>
【题解】[P4178 Tree]
查看>>
Jquery ui widget开发
查看>>
关于indexOf的使用
查看>>
英语单词
查看>>
centos6.8下安装matlab2009(图片转帖)
查看>>
Mongo自动备份
查看>>
enq: SQ - contention
查看>>
cer证书签名验证
查看>>
ant 安装
查看>>
新手Python第一天(接触)
查看>>
iOS中ARC内部原理
查看>>
【bzoj1029】[JSOI2007]建筑抢修
查看>>
synchronized
查看>>
你不得不了解的应用容器引擎---Docker
查看>>
easyui datagrid 弹出页面会出现两个上下滚动条处理办法!
查看>>