当前位置:网站首页>[leetcode 6] zigzag transformation
[leetcode 6] zigzag transformation
2022-04-23 06:24:00 【Don't steal my energy】
Title Description
Will a given string s According to the given number of rows numRows , From top to bottom 、 Left to right Z Font arrangement .( There's a problem with the person who wrote the question , Obviously И Font conversion , I have to say Z)
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 , The 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"
Ideas
1 9 17
2 8 10 16 .
3 7 11 15 .
4 6 12 14 .
5 13 . .
In fact, the string is processed in the above way S Arrange , Then line by line is spliced together to form a new string and return .
string convert(string s, int numRows) {
string ans;
if(numRows==1)
return s;
vector<string> a(numRows);
int i=0;
int flag=0;// Used to interpret the traversal order of the current line 0: From top to bottom 1: From bottom to top
for(int k=0;k<s.length();k++){
if( flag==0 && i<numRows-1 ){
// From top to bottom Did not reach the border
a[i].push_back(s[k]);
i++;
}
else if(flag==0 && i==numRows-1){
// From top to bottom Reach the border
a[i].push_back(s[k]);
flag=1;
i--;
}
else if( flag==1 && i>0 ){
// From bottom to top Did not reach the border
a[i].push_back(s[k]);
i--;
}
else if(flag==1 && i==0){
// From bottom to top Reach the border
a[i].push_back(s[k]);
flag=0;
i++;
}
}
for(int ii=0;ii<numRows;ii++)
ans+=a[ii];
return ans;
}
The title is not difficult. , Mainly thinking , It also tests the structure and operation of data .
版权声明
本文为[Don't steal my energy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210617011920.html
边栏推荐
- Practical operation - Nacos installation and configuration
- A sharp tool to improve work efficiency
- DBCP usage
- MySQL advanced query
- Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images
- POJ - 2955 brackets interval DP
- Graphic numpy array matrix
- IO multiplexing of 09 redis
- 深入理解去噪论文——FFDNet和CBDNet中noise level与噪声方差之间的关系探索
- 程序設計訓練
猜你喜欢
![如何利用对比学习做无监督——[CVPR22]Deraining&[ECCV20]Image Translation](/img/33/780b80693f70112eebc10941f7c134.png)
如何利用对比学习做无监督——[CVPR22]Deraining&[ECCV20]Image Translation

Implementation of displaying database pictures to browser tables based on thymeleaf

MySQL table constraints and table design

St table template

Why does the subscript of the array start from 0 instead of 1?

Addition, deletion, modification and query of MySQL table

自動控制(韓敏版)

Framework analysis 1 Introduction to system architecture

List segmentation best practices
![Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi](/img/1b/4eea05e2634780f45b44273d2764e3.png)
Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi
随机推荐
You cannot access this shared folder because your organization's security policy prevents unauthenticated guests from accessing it
What is the difference between the basic feasible solution and the basic feasible solution in linear programming?
Consistent hash algorithm used for redis cache load balancing
Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising
Installation and usage skills of idea
线性代数第三章-矩阵的初等变换与线性方程组
Linear algebra Chapter 1 - determinant
7.Domino piling
Pytoch -- data loading and processing
Problems and solutions of database migration
Kalman filter and inertial integrated navigation
20 excellent plug-ins recommended by idea
C3p0 database connection pool usage
JSP syntax and JSTL tag
DBCP usage
線性代數第一章-行列式
Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
MySQL best practices for creating tables
去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention
In depth source code analysis servlet first program