当前位置:网站首页>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)
- JS learning notes - first acquaintance
- 忆当年高考|成为程序员的你,后悔了吗?
- How to solve the failure of printer driver installation of computer equipment
- Practice of traffic recording and playback in vivo
- Yyds dry inventory method of deleting expired documents in batch
- Text intelligent expansion and contraction control of swiftui text component (tutorial includes source code)
- Take you ten days to easily complete the go micro service series (I)
- PyC file decompile
- Global and Chinese markets of stainless steel surgical suture 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
Summary | three coordinate systems in machine vision and their relationships
Yyds dry goods inventory student attendance system based on QT design
TypeScript数组乱序输出
潘多拉 IOT 开发板学习(RT-Thread)—— 实验2 RGB LED 实验(学习笔记)
Yyds dry goods inventory has not revealed the artifact? Valentine's Day is coming. Please send her a special gift~
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
忆当年高考|成为程序员的你,后悔了吗?
July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance
Which software is good for machine vision?
How to solve the failure of printer driver installation of computer equipment
随机推荐
Yyds dry inventory method of deleting expired documents in batch
Data security industry series Salon (III) | data security industry standard system construction theme Salon
绝对真理和相对真理思考
[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array
电脑设备打印机驱动安装失败如何解决
Foreign enterprise executives, continuous entrepreneurs, yoga and skiing masters, and a program life of continuous iteration and reconstruction
Global and Chinese markets for disposable insulin pumps 2022-2028: Research Report on technology, participants, trends, market size and share
结构体的内存对齐
Download blender on Alibaba cloud image station
dried food! Understand the structural vulnerability of graph convolution networks
Global and Chinese market of switching valves 2022-2028: Research Report on technology, participants, trends, market size and share
Routing mode: hash and history mode
Sqlserver queries which indexes are underutilized
TCP拥塞控制详解 | 2. 背景
Vscode设置标签页多行显示
Mobile web development learning notes - Layout
mysql数据库mysqldump为啥没有创建数据库的语句
By asp Net core downloads files according to the path exception
LeetCode 3. 无重复字符的最长子串
Unity Json 编写