当前位置:网站首页>Z字形变换
Z字形变换
2022-06-13 09:04:00 【老师好,我是刘同学】
题目描述
力扣Z字形变换
将一个给定字符串 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”
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/zigzag-conversion
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution1:
def convert(self, s:str, numRows:int) -> str:
if numRows < 2:
return s
mat = ["" for _ in range(numRows)]
x = 0
flag = -1
for i in s:
mat[x] += i
if x == 0 or x == numRows - 1:
flag = -flag
x += flag
return "".join(mat)
边栏推荐
- 20211006 integral, differential and projection belong to linear transformation
- How to become a white hat hacker? I suggest you start from these stages
- 20211104 why are the traces of similar matrices the same
- Opencv gaussianblur() explanation (Sigma value)
- Bash: kill: (74001) - operation not allowed
- Completely uninstall PostgreSQL under Linux
- C language: sanziqi
- 教程篇(5.0) 01. 产品简介及安装 * FortiEDR * Fortinet 网络安全专家 NSE 5
- Online debugging tool Arthas advanced
- LeetCode 583. 两个字符串的删除操作
猜你喜欢

消息中间件

C language: deep understanding of character functions and string functions (1)
![[security] how to counter attack from 0 to 1 to become a security engineer with zero Foundation](/img/4d/c33a3fcc3b45c2369e1f1f74a8d51b.png)
[security] how to counter attack from 0 to 1 to become a security engineer with zero Foundation

Use of grep

How many TCP connections can a machine create at most?

JUC atomic reference and ABA problem

What exactly is Huawei cloud desktop saying when it says "smooth"?

Qvector shallow copy performance test

Figure introduction to database neo4j

基于微信小程序的图书馆管理系统.rar(论文+源码)
随机推荐
C language: deep understanding of pointers and arrays
C language 7-13 day K candle chart (15 points)
transforms. ColorJitter(0.3, 0, 0, 0)
教程篇(5.0) 04. Fortint云服务和脚本 * FortiEDR * Fortinet 网络安全专家 NSE 5
Number of parameters of pytorch statistical model
pytorch统计模型的参数个数
20211018 some special matrices
How many TCP connections can a machine create at most?
20211104 why are the traces of similar matrices the same
线上调试工具Arthas基础
Yolov5 model evaluation
JUC 原子累加器 源码之 LongAdder
Message Oriented Middleware
IP address introduction
[QNX hypervisor 2.2 user manual] 4.5 building a guest
批量讀取文件夾下的全部語音文件
JUC原子整数
教程篇(5.0) 01. 产品简介及安装 * FortiEDR * Fortinet 网络安全专家 NSE 5
Class loading overview
Collection of garbled code problems in idea development environment