python的leetcode1324的解法

输入:s = "HOW ARE YOU"
输出:["HAY","ORO","WEU"]
解释:每个单词都应该竖直打印。 
 "HAY"
 "ORO"
 "WEU"


# 本体的巧妙指出,就是本人想到了zip函数的用法,具体的可以去了解。

# 本道题目的想法由leetcode14的解法 https://leetcode-cn.com/problems/longest-common-prefix/联想得到。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/print-words-vertically
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


from typing import List


class Solution:
    def printVertically(self, s: str) -> List[str]:

        s = s.split()
        res = []
        num2 = max(len(x) for x in s)

        for i in s:
            res.append(i.ljust(num2))

        res2 = []
        for i in zip(*res):
            res2.append("".join(i).rstrip(" "))
        return res2


if __name__ == "__main__":
    s = Solution()
    s2 = "TO BE OR NOT TO BE"
    print(s.printVertically(s2))


 

你可能感兴趣的:(Python笔记,python)