68. 文本左右对齐
为保证权益,题目请参考 68. 文本左右对齐(From LeetCode).
解决方案1
Python
python
# 68. 文本左右对齐
# https://leetcode-cn.com/problems/text-justification/
################################################################################
from typing import List
class Solution:
def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
ans = []
l = 0
while l < len(words):
lens = len(words[l])
r = l
while lens <= maxWidth and r + 1 < len(words):
if lens + len(words[r + 1]) + 1 <= maxWidth:
lens += len(words[r + 1]) + 1
r += 1
else:
break
t = ""
if r - l == 0 or r == len(words) - 1:
t = words[l]
for i in range(l + 1, r + 1):
t += " " + words[i]
t += (maxWidth - len(t)) * " "
else:
whiteSpaceLen = maxWidth - (lens - (r - l))
whiteSpaceList = [" " * (whiteSpaceLen // (r - l))] * (r - l)
for i in range(whiteSpaceLen % (r - l)):
whiteSpaceList[i] += " "
t = words[l]
for i in range(r - l):
t += whiteSpaceList[i] + words[l + i + 1]
ans.append(t)
l = r + 1
return ans
################################################################################
if __name__ == "__main__":
solution = Solution()
print(
solution.fullJustify(
["This", "is", "an", "example", "of", "text", "justification."], 16
)
)
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
49
50
51
52
53
54
55
56
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
49
50
51
52
53
54
55
56