当前位置:网站首页>LeetCode 6. Zigzag transformation (n-shaped transformation)
LeetCode 6. Zigzag transformation (n-shaped transformation)
2022-07-02 16:40:00 【_ Liu Xiaoyu】
Will a given string s According to the given number of rows numRows , From top to bottom 、 Left to right Z Font arrangement .
For example, the input string is “PAYPALISHIRING” The number of rows is 3 when , Arranged as follows :
P A H N
A P L S I I G
Y I R
after , Your output needs to be read line by line from left to right , Generate a new string , such as :“PAHNAPLSIIGYIR”.
Please implement this function to transform a string into a specified number of lines :
string convert(string s, int numRows);
Example 1:
Input :s = “PAYPALISHIRING”, numRows = 3
Output :“PAHNAPLSIIGYIR”
Example 2:
Input :s = “PAYPALISHIRING”, numRows = 4
Output :“PINALSIGYAHRPI”
explain :
P I N
A L S I G
Y A H R
P I
Example 3:
Input :s = “A”, numRows = 1
Output :“A”
// This question should be N Glyph conversion
class Solution {
public:
string convert(string s, int numRows) {
string re;
if(numRows == 1) return s;
// Looking for a regular
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]; // Add the front one first
if(k < s.size()) re += s[k];
}
}
}
return re;
}
};
边栏推荐
- Student course selection system (curriculum design of Shandong Agricultural University)
- Global and Chinese markets of stainless steel surgical suture 2022-2028: Research Report on technology, participants, trends, market size and share
- 七一献礼:易鲸捷 “百日会战”完美收官 贵阳银行数据库提前封板
- 一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
- TCP拥塞控制详解 | 2. 背景
- The login box of unity hub becomes too narrow to log in
- Mathematical analysis_ Notes_ Chapter 6: Riemann integral of univariate function
- Original God 2.6 server download and installation tutorial
- JS learning notes - operators
- day4
猜你喜欢

Today in history: Alipay launched barcode payment; The father of time-sharing system was born; The first TV advertisement in the world

What is Amazon keyword index? The consequences of not indexing are serious

Yyds dry inventory company stipulates that all interfaces use post requests. Why?

Take you ten days to easily complete the go micro service series (I)

sql解决连续登录问题变形-节假日过滤

mysql min() 求某条件下最小的值出现多个结果

路由模式:hash和history模式

LeetCode 1. 两数之和

sim2real环境配置教程
![[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)
随机推荐
False summer vacation
电脑管理员权限在哪里可以打开
What is the difference between self attention mechanism and fully connected graph convolution network (GCN)?
MySQL min() finds the minimum value under certain conditions, and there are multiple results
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条...
How to use stustr function in Oracle view
Recalling the college entrance examination and becoming a programmer, do you regret it?
ROW_NUMBER()、RANK()、DENSE_RANK区别
Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation
Set the background picture in the idea (ultra detailed)
2022最新最详细必成功的在Vscode中设置背景图、同时解决不受支持的问题
大厂面试总结大全
Trigger: MySQL implements adding or deleting a piece of data in one table and adding another table at the same time
Maui学习之路(三)--Winui3深入探讨
Mysql database mysqldump why there is no statement to create a database
分析超700万个研发需求发现,这8门编程语言才是行业最需要的!
PCL 点云镜像变换
JS learning notes - operators
Yyds dry inventory method of deleting expired documents in batch
请问怎么在oracle视图中使用stustr函数