Mid/SeniorString
Zigzag Conversion
Given a string s and an integer numRows, write the characters of s in a zigzag pattern on numRows rows.
The zigzag pattern is formed by moving:
- Downward row by row from the top row to the bottom row.
- Then diagonally upward row by row back to the top row.
- Repeating this process until every character in
shas been placed.
Return the string produced by reading the rows of the zigzag pattern from top to bottom.
Example 1
Input
s = "PAYPALISHIRING", numRows = 3Output
"PAHNAPLSIIGYIR"Writing the string in 3 rows and reading row by row gives
PAHNAPLSIIGYIR.Example 2
Input
s = "PAYPALISHIRING", numRows = 4Output
"PINALSIGYAHRPI"Writing the string in 4 rows and reading row by row gives
PINALSIGYAHRPI.Constraints
- 1 <= s.length <= 1000
- s consists of English letters (lower-case and upper-case), ',' and '.'.
- 1 <= numRows <= 1000