Leetcode 1324. Print Words Vertically

2021-08-13 11:10:09 浏览数 (1)

1. Description

2. Solution

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

  • Version 1
代码语言:javascript复制
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/

0 人点赞