成都网站建设司,wordpress禁止右键插件,中山金舜家庭用品有限公司怎样网站地图,网站建设销售兼职合同给定一个单词数组和一个长度 maxWidth#xff0c;重新排版单词#xff0c;使其成为每行恰好有 maxWidth 个字符#xff0c;且左右两端对齐的文本。你应该使用“贪心算法”来放置给定的单词#xff1b;也就是说#xff0c;尽可能多地往每行中放置单词。必要时可用空格 填…给定一个单词数组和一个长度 maxWidth重新排版单词使其成为每行恰好有 maxWidth 个字符且左右两端对齐的文本。你应该使用“贪心算法”来放置给定的单词也就是说尽可能多地往每行中放置单词。必要时可用空格 填充使得每行恰好有 maxWidth 个字符。要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配则左侧放置的空格数要多于右侧的空格数。文本的最后一行应为左对齐且单词之间不插入额外的空格。说明:单词是指由非空格字符组成的字符序列。每个单词的长度大于 0小于等于 maxWidth。输入单词数组 words 至少包含一个单词。示例:输入:
words [This, is, an, example, of, text, justification.]
maxWidth 16
输出:
[This is an,example of text,justification.
]示例 2:输入:
words [What,must,be,acknowledgment,shall,be]
maxWidth 16
输出:
[What must be,acknowledgment ,shall be
]
解释: 注意最后一行的格式应为 shall be 而不是 shall be,因为最后一行应为左对齐而不是左右两端对齐。 第二行同样为左对齐这是因为这行只包含一个单词。示例 3:输入:
words [Science,is,what,we,understand,well,enough,to,explain,to,a,computer.,Art,is,everything,else,we,do]
maxWidth 20
输出:
[Science is what we,understand well,enough to explain to,a computer. Art is,everything else we,do
]#
# lc appleetcode.cn id68 langpython3
#
# [68] 文本左右对齐
## lc codestart
class Solution:def fullJustify(self, words: List[str], maxWidth: int) - List[str]:n len(words)start, end 0, n-1row 0ans [[ for _ in range(maxWidth)] for _ in range(n)]while(startn):total 0num 0pos startwhile(posn):flagFalseif total len(words[pos]) nummaxWidth:flagTruebreaktotallen(words[pos])num1pos1if flag:totalmaxWidth-totalspace,rdivmod(total,max(1,num-1))pp0for i in range(start,startnum):ans[row][pp:pplen(words[i])] words[i]pppplen(words[i])spaceif r0:pp1r-1else:pp0for i in range(start,startnum):ans[row][pp:pplen(words[i])] words[i]pppplen(words[i])1breakstart posrow1tmp []for i in range(row1):tmp.append(.join(ans[i]))return tmp# lc codeend