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