-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.py
37 lines (37 loc) · 1.08 KB
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#coding=utf-8
__author__ = 'xuxuan'
class Solution(object):
def fullJustify(self, words, maxWidth):
"""
:type words: List[str]
:type maxWidth: int
:rtype: List[str]
"""
ansList=[]
a=[]
l=maxWidth
for word in words:
if len(word)<=l:
a.append(word)
l-=(len(word)+1)
else:
l+=len(a)
if len(a)==1:
text=a[0]+' '*l
ansList.append(text)
else:
eachSpaceLength=l/(len(a)-1)
extra=l%(len(a)-1)
text=a[0]
for i in range(1,len(a)):
text+=(' '*(eachSpaceLength+(1 if i<=extra else 0))+a[i])
ansList.append(text)
l=maxWidth-len(word)-1
a=[word]
if len(a)!=0:
text=''
for word in a:
text+=(word+' ')
text=text[:-1]+' '*(l+1)
ansList.append(text)
return ansList