leetcode1324

安稳与你 提交于 2020-01-19 21:14:43
 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

先结算出最长的单词的长度,然后组成二维数组,按照先列后行的方式遍历,并且对每一个新组成的单词进行右边去空格处理。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!