当前位置:网站首页>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;
}
};
边栏推荐
- 中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
- 由ASP.NET Core根据路径下载文件异常引发的探究
- ROW_ NUMBER()、RANK()、DENSE_ Rank difference
- ROW_NUMBER()、RANK()、DENSE_RANK区别
- TypeScript数组乱序输出
- 618深度复盘:海尔智家的制胜方法论
- JS learning notes - process control
- 总结|机器视觉中三大坐标系及其相互关系
- 图书管理系统(山东农业大学课程设计)
- [fluent] dart language (DART language features | JIT instant compilation | AOT static compilation)
猜你喜欢

Vscade set multi line display of tab

IDEA中设置背景图片(超详细)

How to solve the failure of printer driver installation of computer equipment

Idea public method extraction shortcut key

Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)
![[fluent] dart data type string type (string definition | string splicing | string API call)](/img/7b/cc624aa33f45fbed0bbe354253158b.jpg)
[fluent] dart data type string type (string definition | string splicing | string API call)

曆史上的今天:支付寶推出條碼支付;分時系統之父誕生;世界上第一支電視廣告...

Maui学习之路(三)--Winui3深入探讨

Original God 2.6 server download and installation tutorial

Solve * * warning * *: your ApplicationContext is unlikely to start due to a @componentscan of the defau
随机推荐
Today in history: Alipay launched barcode payment; The father of time-sharing system was born; The first TV advertisement in the world
Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)
Maui学习之路(三)--Winui3深入探讨
(practice C language every day) the sum of the nearest three numbers
Comprehensively interpret the background and concept of service mesh
Bone conduction non ear Bluetooth headset brand, bone conduction Bluetooth headset brand recommendation
After the win10 system is upgraded for a period of time, the memory occupation is too high
Boot connection to impala database
数据安全产业系列沙龙(三)| 数据安全产业标准体系建设主题沙龙
SSM integration exception handler and project exception handling scheme
一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
False summer vacation
历史上的今天:支付宝推出条码支付;分时系统之父诞生;世界上第一支电视广告...
注册成为harmonyos开发者并安装DevEco Studio 3.0 Beta2 for HarmonyOS
关于mysql安装的一些问题
PCL least median square method fitting plane
Everyone Xinfu builds: a one-stop intelligent business credit service platform
618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
Recommended practice sharing of Zhilian recruitment based on Nebula graph
Routing mode: hash and history mode