python3 解法
发布于 2021-04-17 02:24 ,所属分类:知识学习综合资讯
class Solution:def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:lenth = 0item = []res = []i = 0while i < len(words):lenth += len(words[i])if lenth <= maxWidth:item.append(words[i])lenth += 1i += 1if i == len(words):item[-1] += ' ' * (maxWidth - lenth + 1)res.append(' '.join(item))else:plus = maxWidth - (lenth - len(words[i]) - len(item))#maxWidth-(lenth-len(words[i])-1)+(len(item)-1)#(lenth-len(words[i])-1)累计的长度#(len(item)-1)固有的单词间的空格if len(item) == 1:item[0] += ' ' * (maxWidth - len(item[0]))else:for j in range(1, len(item)):item[j] = ' ' * int(plus/(len(item) - 1)) + item[j]tmp = plus%(len(item) - 1)for j in range(1, 1+tmp):item[j] = ' ' + item[j]res.append(''.join(item))item = []lenth = 0return res



![[python] 全网最热Python3入门+进阶](https://static.kouhao8.com/sucaidashi/xkbb/7ae6c7e3e4fe91fc43ad7a9f3c1f08a3.jpg?x-oss-process=image/format,webp/resize,w_88/crop,w_88,h_88,g_nw)

















![[Python爬虫] Python3爬虫入门到精通课程视频 附软件、源码、资料 34课](https://static.kouhao8.com/sucaidashi/xkbb/f9fcb42f9657b5654ec86f19109bee3e.jpg?x-oss-process=image/format,webp/resize,w_88/crop,w_88,h_88,g_nw)


相关资源