【Leetcode】z字形变换 @ systemime | 2022-01-28T17:07:18+08:00 | 2 分钟阅读 | 更新于 2022-01-28T17:13:18+08:00

这题弄的我有点自闭,我自认为一开始写的代码比较高效来着,还不如弄循环,看结果直接看 python-2

描述

将一个给定字符串 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);

示例 1:

输入:s = “PAYPALISHIRING”, numRows = 3
输出:“PAHNAPLSIIGYIR”

示例 2:

输入:s = “PAYPALISHIRING”, numRows = 4
输出:“PINALSIGYAHRPI”

解释:

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

示例 3:

输入:s = “A”, numRows = 1
输出:“A”

提示:

1 <= s.length <= 1000
s 由英文字母(小写和大写)、',' 和 ‘.’ 组成
1 <= numRows <= 1000

Related Topics

  • 字符串

👍 1455
👎 0

题解

python-1

"""
5:02 PM	info
		解答成功:
		执行耗时:76 ms,击败了17.81% 的Python3用户
		内存消耗:15.2 MB,击败了35.55% 的Python3用户
"""

class Solution:
    def convert(self, s: str, numRows: int) -> str:

        s_len = len(s)

        if s_len == 1 or numRows == 1 or s_len == numRows:
            return s

        res = ""
        step = 2 * (numRows - 1)
        start, stop = 0, numRows - 1
        for i in range(numRows):

            if i == start or i == stop:
                res += s[i:s_len:step]
                continue

            # 除了首行和尾行,其他行都可以用下面这两个切片获取交叉字符串
            str_1 = s[i:s_len:step]
            str_2 = s[step - i:s_len:step]

            # 合并交叉字符串,上面方法应该很高效,但是这里应该拉低了整个效率
            temp = [""] * (len(str_1) + len(str_2))
            temp[::2] = str_1
            temp[1::2] = str_2
            res += "".join(temp)

        return res

python-2

直接使用数组循环的方式

"""
7:13 PM	info
		解答成功:
		执行耗时:44 ms,击败了92.66% 的Python3用户
		内存消耗:15 MB,击败了93.27% 的Python3用户
"""

class Solution:
    def convert(self, s: str, numRows: int) -> str:

        s_len = len(s)

        if s_len == 1 or numRows == 1 or s_len == numRows:
            return s

        all_save = [""] * numRows

        step = 0
        flag = 1
        for idx, val in enumerate(s):

            all_save[step] += val

            if flag:
                step += 1
            else:
                step -= 1

            if step == numRows - 1 or step == 0:
                flag = 1 - flag

        return "".join(all_save)

© 2018 - 2022 systemime 的博客

Powered by Hugo with theme Dream.

---

avatar
关于我

systemime 的博客

记录一些生活与技术的事或思考

毕业于 🏫 山东科技大学泰山科技学院

目前职位为Python后端开发工程师

热爱代码,热爱开源

主要的技术栈是:

  • python
  • celery
  • django
  • shell
  • sql
  • go
  • nginx

爱好

  • 羽毛球
  • 编码
我的一些开源项目

计划或项目:

  • skill_test ➡️ 一个包含项目常用的django模板:常用脚本、单测方法、数据库连接池、异步请求池,restful风格的回调接口管理器 60%
  • Vbox ➡️ 一个基于k8s和docker的容器云平台,早期项目代码较简单 90%
  • YuQue-Assistant ➡️ 用于批量拉取语雀工作区文章,使用进程池+协程
  • 一个代理池 60%
  • simple_db_pool ➡️ 一个简单数据库连接池 100%
  • 一个电报消息转发脚本 90%
  • 使用flutter做一个app 计划中
  • 其他若干脚本(bilibili、微博图片视频下载、文件对比、图片颜色提取…)
其他

如果你喜欢我的博客、开源项目或者它们可以给你带来帮助,可以赏一杯咖啡 ☕ 给我。~

If you like my open source projects or they can help you. You can buy me a coffee ☕.~

PayPal

https://paypal.me/systemime

支付宝赞赏码

alipay

微信赞赏码

wechat

最好附加一下信息或者留言,方便我可以将捐助记录 📝 下来,十分感谢 🙏。

It is better to attach some information or leave a message so that I can record the donation 📝, thank you very much 🙏.