其他分享
首页 > 其他分享> > leetcode:6. Z 字形变换

leetcode:6. Z 字形变换

作者:互联网

在这里插入图片描述
思路:
简单模拟,注意特殊情况numsrow = 1

src:

class Solution:
    def convert(self, s: str, numRows: int) -> str:
        len1 = len(s)
        cnt = 0
        i = 0
        flag = "down"
        lists = [[] for i in range(numRows)]#二维数组的建立
        while cnt < len1:
            lists[i].append(s[cnt])
            cnt += 1
            #特殊的处理
            if numRows == 1:
                continue
            if i == 0:
                flag = "down"
            if i == numRows - 1:
                flag = "up"
            if flag == "down":
                i += 1
            else:
                i -= 1
        res = ""
        for i in range(numRows):
            res += "".join(lists[i])
        return res

总结:
简单模拟即可

标签:down,cnt,字形,变换,res,lists,numRows,flag,leetcode
来源: https://blog.csdn.net/weixin_40986490/article/details/123197660