当前位置:网站首页>[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
边栏推荐
- Customized communication between threads (reentrantlock)
- Programming training
- Pytoch -- data loading and processing
- 治療TensorFlow後遺症——簡單例子記錄torch.utils.data.dataset.Dataset重寫時的圖片維度問題
- Linear algebra Chapter 1 - determinant
- Kalman filter and inertial integrated navigation
- 自动控制(韩敏版)
- Calculation (enter the calculation formula to get the result)
- 12. Monkeys climb mountains
- Pytorch notes - get familiar with the network construction method by building RESNET (complete code)
猜你喜欢

Gaussian processes of sklearn

线性代数第二章-矩阵及其运算

Generate excel template (drop-down selection, multi-level linkage)
![Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising](/img/cd/10793445e6867eeee613b6ba4b85cf.png)
Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising

You cannot access this shared folder because your organization's security policy prevents unauthenticated guests from accessing it

Detection technology and principle

Kalman filter and inertial integrated navigation

Mysql database foundation

线性代数第一章-行列式

JDBC connection database
随机推荐
The bottom implementation principle of thread - static agent mode
Collections multiple parameter sorting
7.Domino piling
Numpy common function table sorting of data processing
POI and easyexcel exercises
Gaussian processes of sklearn
线性代数第一章-行列式
Guaba and Computational Geometry
Integers have friends interval GCD + double pointer
Troubleshooting of data deleted and reappeared problems
Collection and map thread safety problem solving
How does MySQL convert stored seconds into dates
Chapter 4 of line generation - linear correlation of vector systems
Comparative study paper - [Moco, cvpr2020] momentum contract for unsupervised visual representation learning
12. Monkeys climb mountains
Complete example demonstration of creating table to page - joint table query
Mysql database foundation
Example of ticket selling with reentrant lock
6.Reversal
Understanding and installing MySQL