当前位置:网站首页>6. Z 字形变换
6. Z 字形变换
2022-07-01 03:23:00 【Sun_Sky_Sea】
6. Z 字形变换
原始题目链接:https://leetcode.cn/problems/zigzag-conversion/
将一个给定字符串 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
解题思路:
Z形状的字符赋值排列,输出又是按照行的打印输出,所以一共有numRows,每行的字符串设置为s1,s2。。。sn,每个字符c在Z形状对应的行索引是从s1到sn,然后遇到首行或最后一行,变换方向从sn到s1,这样反复。所以模拟这样的反复操作,计算好每行的字符串,最后将所有行的字符串按照行的顺序链接起来就是题目要求的答案。
代码实现:
class Solution:
def convert(self, s: str, numRows: int) -> str:
# 如果只有一行,直接返回s
if numRows < 2:
return s
# 初始化每行的字符串为空串
# 最后将每行连接起来
res = ['' for _ in range(numRows)]
i = 0
# 反转方向标识:从上到下或从下到上,这里对应的是第几行
flag = -1
for c in s:
# 当前行的字符组织在一起
res[i] += c
# 如果遇到首行或者最后一行,根据Z形状
# 需要换行即换方向
if i == 0 or i == numRows - 1:
flag = -flag
i += flag
return ''.join(res)
参考文献:
https://leetcode.cn/problems/zigzag-conversion/solution/zzi-xing-bian-huan-by-jyd/
边栏推荐
- Avalanche problem and the use of sentinel
- Valentine's Day is nothing.
- 线程数据共享和安全 -ThreadLocal
- Edge drawing: a combined real-time edge and segment detector
- TEC: Knowledge Graph Embedding with Triple Context
- AfxMessageBox和MessageBox的用法
- Md5sum operation
- What happens when a function is called before it is declared in C?
- Appium自动化测试基础--补充:C/S架构和B/S架构说明
- pytorch nn. AdaptiveAvgPool2d(1)
猜你喜欢

The difference between MFC for static libraries and MFC for shared libraries

idea插件备份表

TEC: Knowledge Graph Embedding with Triple Context

How to achieve 0 error (s) and 0 warning (s) in keil5

在线公网安备案保姆级教程【伸手党福利】

Learning notes for introduction to C language multithreaded programming

实现pow(x,n)函数

Avalanche problem and the use of sentinel

还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板

后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动
随机推荐
Database DDL (data definition language) knowledge points
File upload and download
5. [WebGIS practice] software operation - service release and permission management
Pytorch training deep learning network settings CUDA specified GPU visible
4. [WebGIS practice] software operation chapter - data import and processing
Thread data sharing and security -threadlocal
【伸手党福利】开发人员重装系统顺序
Asgnet paper and code interpretation 2
Include() of array
Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
Valid brackets (force deduction 20)
Edlines: a real time line segment detector with a false detection control
Download and installation configuration of cygwin
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
Unexpected token o in JSON at position 1 ,JSON解析问题
Finally in promise
TEC: Knowledge Graph Embedding with Triple Context
ASGNet论文和代码解读2
Go tool cli for command line implementation
Leetcode:剑指 Offer 59 - I. 滑动窗口的最大值