class Solution:
def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
def findlen(level):
count = 0
for l in level:
count += len(l)
return count
n = len(words)
levels = []
level = []
index = 0
while index < n:
if len(level) == 0 and len(words[index]) <= maxWidth:
level.append(words[index])
index += 1
elif len(level) + findlen(level) + len(words[index]) <= maxWidth:
level.append(words[index])
index += 1
else:
levels.append(level)
level = []
levels.append(level)
res = []
for i in range(len(levels)):
temp = ""
length = len(levels[i])
if length == 1:
temp += levels[i][0]
for _ in range(maxWidth - len(levels[i][0])):
temp += " "
res.append(temp)
elif i == len(levels) - 1:
for j in range(length):
temp += levels[i][j]
if j != length - 1:
temp += " "
while len(temp) < maxWidth:
temp += " "
res.append(temp)
else:
block_num = maxWidth - findlen(levels[i])
blocks = [0] * (length - 1)
index = 0
while block_num:
blocks[index] += 1
block_num -= 1
index += 1
if index == len(blocks):
index = 0
for j in range(length):
temp += levels[i][j]
if j < length - 1:
for _ in range(blocks[j]):
temp += " "
res.append(temp)
return res
标签:count,index,level,Python,len,words,对齐,文本
From: https://www.cnblogs.com/DCFV/p/18439111