当前位置:网站首页>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;
}
};
边栏推荐
- dried food! Understand the structural vulnerability of graph convolution networks
- 618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
- 中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
- Mathematical analysis_ Notes_ Chapter 5: univariate differential calculus
- Set the background picture in the idea (ultra detailed)
- unity Hub 登錄框變得很窄 無法登錄
- Leetcode -- number of palindromes
- Global and Chinese market of discharge machines 2022-2028: Research Report on technology, participants, trends, market size and share
- LeetCode 5. 最长回文子串
- 总结|机器视觉中三大坐标系及其相互关系
猜你喜欢

Some problems about MySQL installation

Multi task prompt learning: how to train a large language model?

Mathematical analysis_ Notes_ Chapter 5: univariate differential calculus

历史上的今天:支付宝推出条码支付;分时系统之父诞生;世界上第一支电视广告...

Practice of traffic recording and playback in vivo

La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter

Typescript array out of order output

七一献礼:易鲸捷 “百日会战”完美收官 贵阳银行数据库提前封板
![[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)](/img/c7/1949894e106036d2b412bcd6f70245.jpg)
[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)

Vscade set multi line display of tab
随机推荐
Ranger (I) preliminary perception
unity Hub 登錄框變得很窄 無法登錄
Remove the underline in router link
虚假的暑假
[fluent] dart data type boolean type (boolean type definition | logical operation)
LeetCode 6. Z 字形变换 (N字形变换)
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
Leetcode -- number of palindromes
2022最新最详细必成功的在Vscode中设置背景图、同时解决不受支持的问题
Kubernetes family container housekeeper pod online Q & A?
day4
TCP server communication process (important)
The light of ideal never dies
Sqlserver queries which indexes are underutilized
What is the difference between self attention mechanism and fully connected graph convolution network (GCN)?
LeetCode 2. 两数相加
Mobile web development learning notes - Layout
[error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)
Typescript array out of order output
Sim2real environment configuration tutorial