当前位置:网站首页>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;
}
};
边栏推荐
- Seal Library - installation and introduction
- mysql 计算经纬度范围内的数据
- MySQL min() finds the minimum value under certain conditions, and there are multiple results
- sim2real环境配置教程
- [solution] educational codeforces round 82
- 结构体的内存对齐
- Idea public method extraction shortcut key
- Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)
- Practice of traffic recording and playback in vivo
- Remove the underline in router link
猜你喜欢

微信v3native支付设置的结束时间处理办法

Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)

路由模式:hash和history模式

台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条...

Yyds dry goods inventory has not revealed the artifact? Valentine's Day is coming. Please send her a special gift~

Another graduation season

Boot transaction usage

July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance

JS learning notes - first acquaintance

结构体的内存对齐
随机推荐
PyC file decompile
Solve * * warning * *: your ApplicationContext is unlikely to start due to a @componentscan of the defau
触发器:Mysql实现一张表添加或删除一条数据,另一张表同时添加
云原生的 CICD 框架:Tekton
Memory alignment of structure
ROW_ NUMBER()、RANK()、DENSE_ Rank difference
Summary of monthly report | list of major events of moonbeam in June
2022 the latest and most detailed will successfully set the background image in vscade and solve unsupported problems at the same time
Practice of traffic recording and playback in vivo
The difference and usage of calloc, malloc and realloc functions
Sqlserver queries which indexes are underutilized
Data security industry series Salon (III) | data security industry standard system construction theme Salon
SQLServer查询哪些索引利用率低
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
[fluent] dart data type string type (string definition | string splicing | string API call)
结构体的内存对齐
Storage, reading and writing of blood relationship data of Nepal Graph & Data Warehouse
数学分析_笔记_第5章:一元微分学
Where can I open computer administrator permissions
由ASP.NET Core根据路径下载文件异常引发的探究