LeetCode 68 Text Justification(Python详解及实现)
2017-08-05 12:29
691 查看
【题目】
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)
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)
相关文章推荐
- LeetCode 71 Simplify Path(Python详解及实现)
- LeetCode 60 Permutation Sequence (Python实现及详解)
- LeetCode 78 Subsets (Python详解及实现)
- LeetCode 76 Minimum Window Substring(Python详解及实现)
- 25_LeetCode 82&83. Remove Duplicates from Sorted List(II) (Python详解及实现)
- LeetCode 72 Edit Distance(Python详解及实现)
- LeetCode 65 ValidNumer(Python详解及实现)
- LeetCode 87 Scramble String (Python详解及实现)
- LeetCode 92 Reverse Linked List II (Python详解及实现)
- LeetCode 80 Remove Duplicates from Sorted Array II (Python详解及实现)
- LeetCode 93 Restore IP Addresses(Python详解及实现)
- LeetCode 55 Jump Game(Python 实现及详解)
- LeetCode 89 Gray Code (Python详解及实现)
- LeetCode 84 Largest Rectangle in Histogram (Python详解及实现)
- LeetCode 75 SortColor(Python详解及实现)
- LeetCode 90 SubsetsII (Python详解及实现)
- LeetCode 66 Plus One (Python详解及实现)
- LeetCode 85 Maximal Rectangle (Python详解及实现)
- LeetCode 70 Climbing Stairs(Python详解及实现)
- LeetCode 77 Combinations (Python详解及实现)