68.文本左右对齐
题目描述
文本左右对齐
思路:模拟
统计构成一行的单词后,将该行进行左右对齐的空格补齐。
class Solution:def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:def process(left, right):if right &#61;&#61; n:res &#61; &#39; &#39;.join(words[left:right])res &#43;&#61; &#39; &#39; * (maxWidth - len(res))return resspaces &#61; right - left - 1if not spaces:return words[left] &#43; &#39; &#39; * (maxWidth - len(words[left]))cur &#61; sum(len(w) for w in words[left:right]) &#43; spaceseach &#61; [1] * spacesj &#61; 0while cur < maxWidth:each[j] &#43;&#61; 1j &#43;&#61; 1if j &#61;&#61; spaces:j &#61; 0cur &#43;&#61; 1j &#61; 0res &#61; &#39;&#39;while left < right:res &#43;&#61; words[left]if left < right - 1:res &#43;&#61; &#39; &#39; * each[j]j &#43;&#61; 1left &#43;&#61; 1return resn &#61; len(words)idx &#61; i &#61; 0ans &#61; []while idx < n:i &#61; idxcurLen &#61; len(words[idx])idx &#43;&#61; 1while idx < n and curLen < maxWidth:curLen &#43;&#61; 1curLen &#43;&#61; len(words[idx])idx &#43;&#61; 1if curLen > maxWidth:idx -&#61; 1curLen -&#61; len(words[idx]) &#43; 1ans.append(process(i, idx))return ans