forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0068.py
48 lines (43 loc) · 1.59 KB
/
0068.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
38
39
40
41
42
43
44
45
46
47
48
class Solution:
def fullJustify(self, words, maxWidth):
"""
:type words: List[str]
:type maxWidth: int
:rtype: List[str]
"""
if not words:
return list()
res, row = list(), list()
words_len, count = len(words), 0
i = 0
while i < words_len:
if len(words[i]) + count <= maxWidth:
count += len(words[i]) + 1
row.append(words[i])
i += 1
continue
else:
if len(row) == 1:
res.append(str(row[0])+' '*(maxWidth - len(row[0])))
else:
extraSpace = maxWidth - count + len(row)
num_space = extraSpace//(len(row) - 1)
pre_space = extraSpace%(len(row) - 1)
tmp = ''
for word in row[:-1]:
if pre_space > 0:
tmp += str(word) + ' '*(num_space + 1)
pre_space -= 1
else:
tmp += str(word) + ' '*(num_space + 0)
tmp += str(row[-1])
res.append(tmp)
row.clear()
count = 0
tmp = ' '.join(row)
res.append(tmp + (maxWidth - len(tmp))*' ')
return res
if __name__ == "__main__":
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
print(Solution().fullJustify(words, maxWidth))