题目:
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)
P A H N
A P L S I I G
Y I R
And then read line by line: “PAHNAPLSIIGYIR”
Write the code that will take a string and make this conversion given a number of rows:
string convert(string s, int numRows);
思路:
实际上蕴含着一些规律。把所有answer矩阵中元素的(r,c)坐标(r是行号,c是列号)与对应原字符串s中的位置index(index从0开始)都列出来,你就会发现一个规律:index=r+2*c。是不是很神奇!哈哈哈!
具体分析如下:
注:n就是题目中的numRows。
例如,n为4,字符串s长度为15(具体内容不重要)。其(r,c)与index映射关系如下:
矩阵中的位置(r,c) | 原数组中的位置index |
---|---|
(0,0) | 0 |
(0,3) | 6 |
(0,6) | 12 |
(1,0) | 1 |
(1,2) | 5 |
(1,3) | 7 |
(1,5) | 11 |
(1,6) | 13 |
(2,0) | 2 |
(2,1) | 4 |
(2,3) | 8 |
(2,4) | 10 |
(2,6) | 14 |
(3,0) | 3 |
(3,3) | 9 |
我们可以发现一个规律:index=r+2*c。这样我们就可以知道answer矩阵中每个位置应该输出的元素是什么。
=========================================================================
接下来就要分析矩阵元素的分布规律了。通过画个图就能发现其中的规律。例如,n为7,字符串s长度为31时,分析如下:
规律已经很明显了 。按照图上规律,按行遍历矩阵中所有元素的坐标(不存在就不要遍历),然后将(r,c)转化成index,然后输出元素即可。
代码实现如下,时间复杂度应该为 O ( n ) \ O(n) O(n):
class Solution