美文网首页
LeetCode 6. Z 字形变换(中等)

LeetCode 6. Z 字形变换(中等)

作者: 斐_花小七 | 来源:发表于2020-06-07 17:19 被阅读0次

将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。

比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下:

L C I R E T O E S I I G E D H N 之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"LCIRETOESIIGEDHN"。

class Solution(object):
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        d=[]
        for j in range(numRows):
            d.append('')
        i=0
        reverse=False
        for c in s:
            d[i]+=c
            if numRows!=1:
                i=i-1 if reverse else i+1
                if i==0:
                    reverse=False
                if i==numRows-1:
                    reverse=True
        return (''.join(d))

相关文章

  • LeetCode 6. Z 字形变换(中等)

    将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。 比如输入字符串为 "LEETCODEIS...

  • Python算法-模拟过程实现算法

    6. Z 字形变换[https://leetcode-cn.com/problems/zigzag-convers...

  • 【刷穿 LeetCode】6. Z 字形变换(中等)

    点击 这里[http://wechat.peterxx.com/qr_code_promote.html] 可以查...

  • LeetCode-6 Z字形变换

    题目:6. Z字形变换 难度:中等 分类:字符串 解决方案:字符串遍历 今天我们学习第6题Z字形变换,这是一个字符...

  • LeetCode 6. Z 字形变换

    6. Z 字形变换 题目 将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。比如输入字符串为...

  • leetcode 6. Z 字形变换

    https://leetcode-cn.com/problems/zigzag-conversion/ 做这道题,...

  • LeetCode 6. Z 字形变换

    题目 将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。 示例:输入: s = "LEETC...

  • [leetcode]6. Z 字形变换

    题目描述: 将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。比如输入字符串为 "LEETC...

  • LeetCode 6. Z 字形变换

    6. Z 字形变换 将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。比如输入字符串为 "L...

  • LeetCode 6. Z 字形变换

    将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。 比如输入字符串为 "LEETCODEIS...

网友评论

      本文标题:LeetCode 6. Z 字形变换(中等)

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