leetcode68. Text Justification

xiaoxiao2021-02-27  394

参考https://github.com/mission-peace/interview/blob/master/src/com/interview/dynamic/TextJustification.java

https://www.youtube.com/watch?v=RORuwHiblPc

class Solution(object):     def fullJustify(self, words, maxWidth):         """         :type words: List[str]         :type maxWidth: int         :rtype: List[str]         """         res, cur, num_of_letters = [], [], 0         for w in words:             if num_of_letters + len(w) + len(cur) > maxWidth:                 if len(cur) == 1:                     res.append( cur[0] + ' '*(maxWidth - num_of_letters) )                 else:                     num_spaces = maxWidth - num_of_letters                     space_between_words, num_extra_spaces = divmod( num_spaces, len(cur)-1)                     for i in range(num_extra_spaces):                         cur[i] += ' '                     res.append( (' '*space_between_words).join(cur) )                 cur, num_of_letters = [], 0             cur += [w]             num_of_letters += len(w)         res.append( ' '.join(cur) + ' '*(maxWidth - num_of_letters - len(cur) + 1) )         return res

转载请注明原文地址: https://www.6miu.com/read-2459.html

最新回复(0)