Leetcode 1324. Print Words Vertically

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Print Words Vertically

2. Solution

解析:Version 1,先将字符串按空格分开,然后找到最长的子串长度,遍历长度构造结果子串,注意每个子串后面的空格要去掉。

  • Version 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
def printVertically(self, s: str) -> List[str]:
result = []
words = s.split(' ')
n = max(list(map(len, words)))
for i in range(n):
temp = ''
for word in words:
if i < len(word):
temp += word[i]
else:
temp += ' '
result.append(temp.rstrip())
return result

Reference

  1. https://leetcode.com/problems/print-words-vertically/
如果有收获,可以请我喝杯咖啡!