当前位置: 代码迷 >> 综合 >> Leetcode 1324. Print Words Vertically
  详细解决方案

Leetcode 1324. Print Words Vertically

热度:57   发布时间:2023-12-12 20:53:15.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/
  相关解决方案