6. ZigZag Conversion [easy] (Python)

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

题目链接

https://leetcode.com/problems/zigzag-conversion/

题目原文

The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R

And then read line by line: “PAHNAPLSIIGYIR”
Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows);
convert(“PAYPALISHIRING”, 3) should return “PAHNAPLSIIGYIR”.

题目翻译

zigzag字符串转换,具体的规则见题目原文,太长不翻译了。。。
要写的函数接收两个参数,要转换的字符串text和zigzag的行数nRows,返回转换后的字符串。

思路方法

思路一

模拟书写zigzag字符串的过程。定义一个有numRows个元素的数组,每个元素初始是空字符串,代表numRows个行字符串的初始情况;然后扫描输入字符串s,依次将每个字符添加到相应行字符串的末尾;最后将所有行字符串拼接即得结果。
注意,代码中用了取模操作来判断:是否到了需要换一个方向书写zigzag字符的时候。

代码

class Solution(object):
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if numRows <= 1 or numRows >= len(s):
            return s
        arr = [''] * numRows
        line, step = 0, -1
        for c in s:
            arr[line] += c
            if line % (numRows-1) == 0:
                step = - step
            line += step
        return ''.join(arr)

思路二

如果稍微考虑的数学一点,那么s中的第i个字符(下标从第0个开始),如果按照zigzag书写方式会出现在的行数为(行数为0到numRows-1行):
i % (2 * numRows - 2), if i % (2 * numRows - 2) < numRows
2 * numRows - 2 - (i % (2 * numRows - 2)), if i % (2 * numRows - 2) >= numRows
有了这个结果,对于任意一个位置的字符我们都知道它应该在第几行。下面的代码仍然是顺序扫描原字符串s,当然也可以有别的办法。

代码

class Solution(object):
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if numRows <= 1 or numRows >= len(s):
            return s
        arr = [''] * numRows
        for i in xrange(len(s)):
            tmp = i % (numRows + numRows - 2)
            if tmp < numRows:
                arr[tmp] += s[i]
            else:
                arr[numRows + numRows - 2 - tmp] += s[i]
        return ''.join(arr)

PS: 新手刷LeetCode,新手写博客,写错了或者写的不清楚还请帮忙指出,谢谢!
转载请注明:http://blog.csdn.net/coder_orz/article/details/52039689

Z字形扫描zigzag二维矩阵python实现 在对图像进行 dct 变换后,原图像的高频分量集中在左上,低频部分集中在右下。在此情况下,将图像将成一维,用简单的逐行逐列效果不会很好,故需用Z字形扫描的方式进行展开。下图给出了奇数行列和偶数行列的情况示意图(仅供参考,行列数可以不相等,但一般图像处理里,分块都是8*8的小块)。 下面给出了代码,注释写得比较清楚。请结合图像理解。几个关键点: 1、在不考虑边界时,根据行和列数和是奇数还是偶数来区分前面的方向是右上还是左下; 2、在考虑边界时,需要转弯,转弯时需要判断是哪个边界、或者顶点位置; 阅读详情

相关推荐

zigzag算法的python版本

zigzag算法的python版本,支持非正方形图像的zigzag列表生成,包含ZigZag()和deZigZag()函数,后者可实现原图重组。

python 实现zigzag排列

最近在写zigzag排列的时候,感觉还是有点难写的,主要是判断条件有点多。所以就放到这里做一下笔记。 def zigzag(data): # 输入应该是np.array类型的数组 (r, c) = data.shape if(r != c): print("行和列应该一样!") return 0 ZZ = zeros((1, r*c)) p = 0 for index in range(2 * r): if(ind

wuzhongqing12的博客 2420

ZigzagPoints - 指标 Zigzag 测量方法的脚本 - MetaTrader 4脚本.zip

可以测量所有加入 ZigZag breaks的部分,其结果将以点的形式给出。

Python量化:ZIGZAG之字转向

记得刚入行那会儿,我在营业部给客户讲解技术指标,看到ZIGZAG这个名称时差点没憋住笑。这名字也太形象了吧?就像小朋友画的山峰一样,上上下下拐来拐去。后来才发现,这个看似简单的指标,在量化交易里居然是个宝藏。上周有个程序员客户来找我开户,开口就问:"你们API支持实时获取ZIGZAG转向点吗?"我当时就眼前一亮——这是个懂行的。现在做量化的,不会用ZIGZAG过滤噪音,都不好意思说自己搞程序化交易。

wencaitouzi的博客 1793

ZigZag 开源项目指南

ZigZag 开源项目指南 1、项目介绍 ZigZag 是一个强大的工具库,用于处理复杂的序列数据并将其转换成更易于操作的形式。该项目在GitHub上开放源代码,旨在提供一套丰富的API接口,以实现对序列数据的高效管理和操作。无论是对于大数据分析还是实时系统监控,ZigZag 都可以成为一个不可或缺的组件。 2、项目快速启动 要开始使用 ZigZag,您首先需要将这个库集成到您的项目中。以下是通过...

gitblog_00535的博客 1301

[LeetCode] 006. ZigZag Conversion (Easy) (C++/Java/Python)

[LeetCode] 006. ZigZag Conversion (Easy) (C++/Java/Python)

水果君の日常 3205

[Leetcode 6, Easy] Zigzag Conversion

Leetcode, Easy

算法的实践 445

LeetCode6. ZigZag ConversionPython

Problem 字符串&amp;quot;PAYPALISHIRING&amp;quot;在给定行数上以Z字形图案写入,如下所示:(您可能希望以固定字体显示此图案以获得更好的易读性) 然后逐行阅读: “PAHNAPLSIIGYIR” 编写将采用字符串的代码并在给定多行的情况下进行此转换: string convert(string s,int numRows); 例1: 输入: s =“PAYPALISHIRING”,numRow...

E.W的博客 2283

LeetCodeZigZag Conversion 题解

6. ZigZag Conversion Total Accepted: 82952 Total Submissions: 349151 Difficulty: Easy The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like th

SDUTACM的博客 588

LeetCode with PythonZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIGYIR

XNERV SURVEYS 1万+

ZIG指标

里面有要求与我自己编的程序,麻烦大家看看哪里有问题

zigzag的matlab实现

用matlab实现zigzag,将八成八的矩阵按zigzag化成向量

leetcode 6 ZigZag Conversion

The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P—–A—–H—–N A–P–L–S–I—I–G

shiter编写程序的艺术 1699

Leetcode: ZigZag Conversion

最近在改论文,不喜欢写论文,但是为了毕业也没有办法!尽自己最大的努力做到最好吧! 这道题目做完貌似所有的Easy级别的题目就做完了,开始Medium的题目!加油吧! 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may wan

给永远比拿愉快 1126

[编程练习]ZigZag Conversion <LeetCode-6>

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I

drowzju的专栏 538

LeetCode】修炼之路-0006-Zigzag Conversion (Z 字形变换)【python

列表生成式属于可以体现the zen of python的特性之一,理解好列表生成式,用好列表生成式,可以让你的代码更清晰,建议多看,多练习,掌握列表生成式的使用方法,可以让你的代码更紧凑,可读性更好,代码更优雅。

crazyjinks的博客 1304
上一篇: 1. Two Sum [easy] (Python)
下一篇: 7. Reverse Integer [easy] (Python)
coder_orz
博客等级 码龄14年 102粉丝 90原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值