#6
Zigzag Conversion
specialist · 890 · lc medium +31 · verified · 53.6% accepted · 9,285 likes · top 45%
Description
When the characters of a string are laid out in a zigzag pattern across numRows rows and then read row by row, a new string results. Implement convert(s, numRows) to perform this transformation.
Example 1:
P A H N
A P L S I I G
Y I R
Example 2:
string convert(string s, int numRows);
Example 3:
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Example 4:
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
Example 5:
Input: s = "A", numRows = 1
Output: "A"
Code
1
2
3