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”.
解法1:O(N) Time + O(N) Space
这题的数学解法很难想,也比较花时间。很直观的做法倒是不错。就每一行用一个stringbuilder,然后一行行的塞。先从上往下,再从下往上斜着填。
当所有数字填满了之后把stringbuilder连起来就可以了。
C++
Java