leetcode1324
Posted AsenYang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode1324相关的知识,希望对你有一定的参考价值。
1 class Solution: 2 def printVertically(self, s: str) -> ‘List[str]‘: 3 words = s.split(‘ ‘) 4 matrix = [] 5 maxlen = 0 6 for w in words: 7 maxlen = max(maxlen,len(w)) 8 for word in words: 9 matrix.append(word + ‘ ‘ * (maxlen - len(word))) 10 result = [] 11 for j in range(maxlen): 12 temp = ‘‘ 13 for i in range(len(words)): 14 temp += matrix[i][j] 15 result.append(temp.rstrip()) 16 return result
先结算出最长的单词的长度,然后组成二维数组,按照先列后行的方式遍历,并且对每一个新组成的单词进行右边去空格处理。
以上是关于leetcode1324的主要内容,如果未能解决你的问题,请参考以下文章
Leetcode 1324. Print Words Vertically
leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段