您当前的位置: 首页 >  leetcode

孑渡

暂无认证

  • 2浏览

    0关注

    178博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Leetcode】中等题:Z 字形变换

孑渡 发布时间:2022-04-14 15:31:49 ,浏览量:2

Z 字形变换

将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。 比如输入字符串为 “PAYPALISHIRING” 行数为 3 时,排列如下: P A H N A P L S I I G Y I R 之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:“PAHNAPLSIIGYIR”。 请你实现这个将字符串进行指定行数变换的函数: string convert(string s, int numRows); 来源:力扣(LeetCode)

AC代码
class Solution:
    def convert(self, s: str, numRows: int) -> str:
        if numRows == 1:
            return s
        temp = ['' for i in range(numRows)]
        length = len(s)
        res = ''
        for i in range(numRows):
            if i == 0:
                j = 0
                while j * numRows - j = len(s):
            return s
        mat = [[] for _ in range(r)]
        t, x = r * 2 - 2, 0
        for i, ch in enumerate(s):
            mat[x].append(ch)
            x += 1 if i % t             
关注
打赏
1663211900
查看更多评论
0.0375s