Description
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
1 | P A H N |
Write the code to convert a string to a zigzag pattern.
You should do it in-place.
Example 1:
1 | Input: s = "PAYPALISHIRING", numRows = 3 |
Example 2:
1 | Input: s = "PAYPALISHIRING", numRows = 4 |
Approach
We can solve this problem by iterating over the string and keeping track of the current row and direction. We can use two pointers, one for the current row and one for the direction. We can then iterate over the string and add each character to the appropriate row based on the current row and direction.
1 | class Solution: |







