您的位置:首页 > 其它

leetcode 68. Text Justification

2016-06-29 17:16 183 查看
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.

给定一个单词数组,和每行的字符数,按照要求匹配

首先判断下每行:

当前tmp中存储的单词的长度flag+该单词长度<=maxWidth

如果满足,则将该单词加入到tmp中

如果不满足,tmp数组直接进行规范化(modify()函数)

注意到最后一段是按照正常的输出格式,少的部分进行空格补全

class Solution(object):
def modify(self,tmpwords,maxWidth):
"""
:type tmpwords:List[str] eg:['This','is','an']
:type maxWidth: int
"""
word_count=len(tmpwords)
space_count=max(1,word_count-1)
character_num=sum([len(i) for i in tmpwords])
base_space=(maxWidth-character_num)/space_count
extra_space=(maxWidth-character_num)%space_count
ret=''
for i in range(space_count):
ret=ret+tmpwords[i]+''.join([' ']*base_space)
if i<extra_space:
ret=ret+' '
if word_count>1:
ret=ret+tmpwords[-1]
return ret
def fullJustify(self, words, maxWidth):
"""
:type words: List[str]
:type maxWidth: int
:rtype: List[str]
"""
result=[]
tmp=[]
flag=0
for i in words:
if flag+len(i)<=maxWidth:
tmp.append(i)
flag=flag+len(i)+1
else:
if len(tmp):
result.append(self.modify(tmp,maxWidth))
tmp=[i]
flag=len(i)+1
ans=''
for i in tmp[0:len(tmp)-1]:
ans=ans+i+' '
if len(tmp):
ans=ans+tmp[-1]
ans=ans+''.join([' ']*(maxWidth-len(ans)))
result.append(ans)
return result
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode