当前位置:网站首页>Leetcode 6. Zigzag transformation (awesome, solved)
Leetcode 6. Zigzag transformation (awesome, solved)
2022-06-28 17:21:00 【I'm not xiaohaiwa~~~~】

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"
Tips :
- 1 <= s.length <= 1000
- s By the English letters ( Lowercase and upper case )、‘,’ and ‘.’ form
- 1 <= numRows <= 1000
Code:
class Solution {
public:
string convert(string s, int numRows) {
if(numRows==1)
return s;
string res;
int col=0;
int loop=0;
int row=numRows;
vector<vector<char>> newOne(numRows, vector<char>(10000, ' '));
do
{
//if(col)
if(row==numRows)
{
for(int i=0;i<row;i++)
{
newOne[i][col]=s[loop++];
if(loop==s.length())
{
loop=0;
for(int m=0;m<newOne.size();m++)
{
vector<char>sub=newOne[m];
for(int n=0;n<sub.size();n++)
{
if(sub[n]!=' ')
{
loop++;
res+=sub[n];
}
// cout<<sub[n]<<" ";
if(loop==s.length())
return res;
}
// cout<<endl;
}
return res;
}
}
}
else
{
newOne[row-1][col]=s[loop++];
if(loop==s.length())
{
loop=0;
for(int m=0;m<newOne.size();m++)
{
vector<char>sub=newOne[m];
for(int n=0;n<sub.size();n++)
{
if(sub[n]!=' ')
{
loop++;
res+=sub[n];
}
// cout<<sub[n]<<" ";
if(loop==s.length())
return res;
}
// cout<<endl;
}
return res;
}
}
col++;
if(row>1)
row--;
else
row++;
if(row==1)
{
row=numRows;
}
}while(1);
return res;
}
};
边栏推荐
- NoSQL之Redis配置与优化(你不在南京的日子我替你吹了秦淮河的晚风)
- AUTOSAR software development training
- 本地部署Confluence遇到的问题:MySQL数据库编码、隔离级别、验证码不显示
- Which securities company is better and safer to choose for opening an exchange fund account by mobile phone
- 节点基础~节点层级
- Batchnorm2d principle, function and explanation of batchnorm2d function parameters in pytorch
- [tcapulusdb knowledge base] modify business modify cluster
- Leetcode 6. Z 字形变换(牛逼,解决了)
- IP problem occurs when a virtual system is cloned in VirtualBox
- IDC:阿里云获2021中国数据治理平台市场份额第一
猜你喜欢

Gestion des journaux sauvegarde et récupération des journaux dans MySQL

这个简单的小功能,半年为我们产研团队省下213个小时

StackOverflow 2022 开发者报告:PostgreSQL 超越 MySQL !

'summary of common network protocols' summarized by Tsinghua leaders in 3 days

MySQL High available MHA (accidentellement avec ma jeunesse)

ICML 2022 | transferable imitation learning method based on decoupling gradient optimization

PotPlayer播放百度云盘视频

听说转行软件测试只能自学,培训机构是个坑?

IDC:阿里云获2021中国数据治理平台市场份额第一

12 SQL optimization schemes summarized by old drivers (very practical)
随机推荐
55. 连续子数组的最大和
offsetwidth\clientwidth\scrollwidth
win10用cmake3.22与vs2019编译curl库源码并调用
offsetwidth\clientwidth\scrollwidth
ZABBIX monitoring of traditional operation and maintenance (you get up early, I get up early, and we will get together sooner or later)
Tacacs+服务器部署
Flex layout
Win10 compiles curl library source code with cmake3.22 and vs2019 and calls
VirtualBox中克隆了一个虚拟系统出现IP问题
[daily 3 questions (1)] the second largest number in the string
PotPlayer播放百度雲盤視頻
ICML 2022 | transferable imitation learning method based on decoupling gradient optimization
Cross cluster deployment of helm applications using karmada
It's said that software testing for career change can only be self-taught. Is the training institution a pit?
Tacacs+ server deployment
这个简单的小功能,半年为我们产研团队省下213个小时
58技术沙龙第三十一期|Flutter动态化专题沙龙
MySQL High available MHA (accidentellement avec ma jeunesse)
12 SQL optimization schemes summarized by old drivers (very practical)
基于DataWorks的时效仿真平台|得物技术