LeetCode-68 Text Justification

Description

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

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

Submissions

解题思路为,首先初始化res, line, count三个变量,res代表最后的结果集, line代表每一行存储的单词, count代表当前line的单词总长度. 然后从头遍历words,由于每个单词之间至少有一个空格,所以判断line的单词个数和单词总长度加上当前遍历单词的长度是否大于题目给出的最大宽度maxWidth,如果大于则代表溢出,当前遍历单词应放到下一line,并给当前line从左到右插入空格,以保证空格不能平均分配时,左侧添加更多的空格.之后重置line和count,进行分配下一line.并利用join方法连接line.并在最后用到ljust方法,保证最后一行左对齐.

Python ljust() 方法返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
ljust()方法语法:str.ljust(width[, fillchar])
参数:width – 指定字符串长度。fillchar – 填充字符,默认为空格。

实现代码如下:

class Solution:
    def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
        res, line, count = [], [], 0
        for i in words:
            if len(line) + count + len(i) > maxWidth:
                for j in range(maxWidth-count):
                    line[j % max(len(line)-1, 1)] += ' '
                res.append(''.join(line))
                line, count = [], 0
            line += [i]
            count += len(i)
        return res + [' '.join(line).ljust(maxWidth)]

参考

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值