当前位置:网站首页>LeetCode 6. Z 字形变换 (N字形变换)
LeetCode 6. Z 字形变换 (N字形变换)
2022-07-02 13:15:00 【_刘小雨】
将一个给定字符串 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”
// 此题应该是N 字形转换
class Solution {
public:
string convert(string s, int numRows) {
string re;
if(numRows == 1) return s;
// 找规律
for(int j =0; j < numRows; j++)
{
if(j == 0 || j == numRows -1 )
{
for(int i = j; i<s.size(); i += 2 * numRows - 2)
re += s[i];
}
else
{
for(int i = j, k = 2 * numRows -2 - j; i < s.size() || k < s.size() ;i += 2 * numRows -2, k += 2 * numRows -2)
{
if(i < s.size()) re += s[i]; // 先加前面的
if(k < s.size()) re += s[k];
}
}
}
return re;
}
};
边栏推荐
- Boot transaction usage
- What is the difference between self attention mechanism and fully connected graph convolution network (GCN)?
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验2 RGB LED 实验(学习笔记)
- Classifier visual interpretation stylex: Google, MIT, etc. have found the key attributes that affect image classification
- Does bone conduction earphone have external sound? Advantages of bone conduction earphones
- Mathematical analysis_ Notes_ Chapter 6: Riemann integral of univariate function
- Recommended practice sharing of Zhilian recruitment based on Nebula graph
- (practice C language every day) the sum of the nearest three numbers
- Idea public method extraction shortcut key
- Bib | graph representation based on heterogeneous information network learning to predict drug disease association
猜你喜欢
Maui learning road (III) -- in depth discussion of winui3
sim2real环境配置教程
Yyds dry inventory method of deleting expired documents in batch
Compress words (kmp/ string hash, double hash)
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
[fluent] dart data type string type (string definition | string splicing | string API call)
Mathematical analysis_ Notes_ Chapter 5: univariate differential calculus
一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
Recalling the college entrance examination and becoming a programmer, do you regret it?
电脑设备打印机驱动安装失败如何解决
随机推荐
sql解决连续登录问题变形-节假日过滤
触发器:Mysql实现一张表添加或删除一条数据,另一张表同时添加
Summary of monthly report | list of major events of moonbeam in June
Memory alignment of structure
Data security industry series Salon (III) | data security industry standard system construction theme Salon
HMS core machine learning service helps zaful users to shop conveniently
AWS virtual machine expansion
The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 releases | geek headlines
Aike AI frontier promotion (2.15)
PCL least median square method fitting plane
自注意力机制和全连接的图卷积网络(GCN)有什么区别联系?
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
路由模式:hash和history模式
The difference and usage of calloc, malloc and realloc functions
The light of ideal never dies
Idea public method extraction shortcut key
Write your own CPU Chapter 11 - learning notes
SQL solves the problem of continuous login deformation holiday filtering
Boot transaction usage
Conditions and solutions of deadlock