当前位置:网站首页>Leetcode:06zigzag transformation
Leetcode:06zigzag transformation
2022-07-21 18:45:00 【Still work hard!】
Z Font conversion
subject
Will a given string s s s According to the given number of rows n u m R o w s numRows 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 :
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
Method 1 : Analog transformation process
Analysis topic , take Z The glyph is regarded as a two-dimensional matrix , The process of transformation is simply described as : Fill the characters in the string into the two-dimensional matrix in turn , The filling order is from [0][0] Start to go down , fill numRows That's ok , And then according to [ Row number -1][ Number of columns +1] Fill in one character at a time , until Row number =0, Then fill in , fill numRows After that, follow [ Row number -1][ Number of columns +1] Fill in one character at a time , Cycle until all the characters in the string are filled , Finally, the final string is sorted out in the way of line reading .
Realization way
Use one Vector Container to store the elements of each row , The final Z Glyph total numRows That's ok , So first initialize a containing numRows An element of Vector, according to Z The order of font transformation , Put the second in turn i Put the characters of the line into Vector No i Of the elements , In the end Vector The strings in each element of the are spliced into the final result .
Set an index vIndex stay [0,numRows-1] Move back and forth between , Indicates the number of rows of the current character in the two-dimensional matrix , The characters in the string are added to Vector No vIndex Of the elements .
Code implementation
class Solution {
public:
string convert(string s, int numRows) {
// Used to traverse the string s The index of
int index = 0;
int size = s.length();
if (numRows == 1 || numRows >= size) {
cout << s << endl;
return 0;
}
vector<string> nRes(numRows, "");
//vIndex Used to point to the number of rows of the current element in the two-dimensional matrix , The same is Vector The element index of
int vIndex = 0;
// Judge whether the current row index should be increased or decreased
bool add = true;
while (index < size)
{
// From 0 Fill in numRows Elements
while (index < size && add) {
nRes[vIndex] += s[index];
vIndex++;
index++;
if (vIndex == numRows) {
add = false;
}
}
vIndex -= 2;
// according to [ Row number -1][ Number of columns +1] Fill in one character at a time , until Row number =0
while (!add && index < size) {
nRes[vIndex] += s[index];
vIndex--;
index++;
if (vIndex == -1) {
add = true;
}
}
vIndex += 2;
}
string res = "";
for (string var : nRes)
{
res += var;
}
return res;
};
Read the solution on Li Kou , Also found the law of transformation , And use mathematical formulas to express , The idea is similar to , Can be used as a reference .
边栏推荐
- How much has changed from new retail to community group purchase?
- Registry introduction and maintenance
- It is not just products that keep pace with the times. Look at FAW Toyota's confidence in "Second Entrepreneurship" from Carola Ruifang
- C语言力扣第32题之最长有效括号。用栈实现
- Golang collection: custom types and method sets
- 循环结构--while循环和do-while循环
- Technology Encyclopedia | cloud native
- 小程序的破局之道,数字化营销已然成为趋势
- c语言入门---操作符
- College student community management system based on SSM framework
猜你喜欢
Application cases and common technologies of digital twins
Involution: Inverting the Inherence of Convolution for Visual Recognition(CVPR2021)
pytorch利用nn.unfold重新实现卷积操作
Tiflash source code reading (V) deltatree storage engine design and implementation analysis - Part 2
微信小程序 图片src变量拼接问题
Zero basic learning canoe panel (1) -- new panel
解决Kettle8.2版本表输入-Excel输出时,时间字段空白
Polyethyleneimine (PEI) modified mil-101 (CR) | polyacid based metal organic framework material (pom-mof) | ferrocene modified mil-88b | Qiyue biology
ctfshow web入门(文件包含)
Application of carboxymethyl fluorescein 6-fam modified PNA peptide nucleic acid 6-fam-pna|cy3-pna fluorescent dye Cy3 coupling PNA peptide nucleic acid
随机推荐
Leetcode 201 Digit range bitwise and (2022.07.20)
It is not just products that keep pace with the times. Look at FAW Toyota's confidence in "Second Entrepreneurship" from Carola Ruifang
Kwai wants to grab the shell cake. Is online selling a good business?
解决Kettle8.2版本表输入-Excel输出时,时间字段空白
C language practice topic + answer: 26-30
技术小百科 | 云原生篇
微信小程序 图片src变量拼接问题
从部队文职转行程序员,我有这些体会
[04] through the power consumption wall, what aspects should we improve "performance"?
A little learning of JNDI injection
C语言经典实例:21-30例:插入排序、希尔排序1、快速排序、希尔排序2、递归法、完数、斐波那契数列、公约数和公倍数、判断水仙花数统计单词个数
[notes for question brushing] factorial mantissa
小程序的破局之道,数字化营销已然成为趋势
Technology Encyclopedia | cloud native
[IOT design. 2] stm32f407 single chip microcomputer + smart cloud aiot+ overall scheme design of pig house monitoring and system
Metal organic framework mil-100 (CR) and mil-101 (CR) Supported Phosphotungstic Acid | zirconium based metal organic framework [email pr
FeCl3 modified mil-101 (CR) ferric chloride | zirconium metal organic framework compound (uio-68-sazide) | cobalt based metal organic framework (zif-67) | MOF custom material
Okaleido tiger NFT即将登录Binance NFT平台,NFT权益时代即将开启
【04】穿越功耗墙,我们该从哪些方面提升“性能”?
数字孪生社区管理系统,九大应用场景建设