美文网首页
Leetcode 1324. Print Words Verti

Leetcode 1324. Print Words Verti

作者: SnailTyan | 来源:发表于2021-09-17 14:41 被阅读0次

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

1. Description

Print Words Vertically

2. Solution

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

  • Version 1
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/

相关文章

网友评论

      本文标题:Leetcode 1324. Print Words Verti

      本文链接:https://www.haomeiwen.com/subject/eyecbltx.html