当前位置:网站首页>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;
}
};
边栏推荐
- Yyds dry inventory method of deleting expired documents in batch
- How to use stustr function in Oracle view
- Construction and business practice of Zhongke brain knowledge map platform
- Conditions and solutions of deadlock
- 路由模式:hash和history模式
- 由ASP.NET Core根据路径下载文件异常引发的探究
- OSPF - detailed explanation of NSSA area and full NSSA area (including configuration command), LSA type 7 lsa-7
- 中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
- False summer vacation
- Leetcode -- number of palindromes
猜你喜欢

触发器:Mysql实现一张表添加或删除一条数据,另一张表同时添加

Vscade set multi line display of tab

SQL solves the problem of continuous login deformation holiday filtering

JS learning notes - process control

Effectively use keywords to increase Amazon sales

Construction and business practice of Zhongke brain knowledge map platform

PyC file decompile
![[Yu Yue education] reference materials of sensing and intelligent control technology of Nanjing University of Technology](/img/5c/5f835c286548907f3f09ecb66b2068.jpg)
[Yu Yue education] reference materials of sensing and intelligent control technology of Nanjing University of Technology

SSM整合-异常处理器及项目异常处理方案

注册成为harmonyos开发者并安装DevEco Studio 3.0 Beta2 for HarmonyOS
随机推荐
Boot connection to impala database
潘多拉 IOT 开发板学习(RT-Thread)—— 实验2 RGB LED 实验(学习笔记)
原神2.6服务端下载以及搭建安装教程
去除router-link中的下划线
微信v3native支付设置的结束时间处理办法
End time processing method of wechat v3native payment settings
结构体的内存对齐
Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)
Data security industry series Salon (III) | data security industry standard system construction theme Salon
绝对真理和相对真理思考
[Yu Yue education] reference materials of sensing and intelligent control technology of Nanjing University of Technology
PCL 最小中值平方法拟合平面
请问怎么在oracle视图中使用stustr函数
Yyds dry goods inventory has not revealed the artifact? Valentine's Day is coming. Please send her a special gift~
数学分析_笔记_第5章:一元微分学
Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
How to solve the failure of printer driver installation of computer equipment
Add user-defined formula (time sharing t+0) to mobile app access as an example
Which software is good for machine vision?
JS learning notes - first acquaintance