
Problem
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)
|
|
And then read line by line: “PAHNAPLSIIGYIR”
Write the code that will take a string and make this conversion given a number of rows:
|
|
convert(“PAYPALISHIRING”, 3) should return “PAHNAPLSIIGYIR”.
Analysis
- n = number of raws
- For first and last line, step = 2n-2
- For ith lines (except the first and the last), step = 2n-2-2*i, 2n-2
Python Implementation
|
|




近期评论