题目
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 text, int nRows);
convert(“PAYPALISHIRING”, 3) should return “PAHNAPLSIIGYIR”.
- 大意:把给定的一个字符串,写成指定行数的N字形(ZigZag形),然后从左到右读成新的字符串:
思路和吐槽
这道题还是没有什么高深的,只是要去找规律,推通式,我再次鄙视下自己的智商。
##代码
###Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
class Solution(object): def convert(self, s, numRows): <!--more--> """ :type s: str :type numRows: int :rtype: str """ max_index = len(s) ans = [] T = 2 * (numRows - 1) if numRows == 1 or max_index <= numRows: return s i = 0 while(True): if i == 0: j = 0 while(True): if j * T + i < max_index: ans.append(s[j * T + i]) j += 1 else: break elif i == numRows - 1: j = 0 while(True): if j * T + i < max_index: ans.append(s[j * T + i]) j += 1 else: break ans_str = ''.join(ans); return ans_str else: j = 0 while(True): if j * T + i < max_index: ans.append(s[j * T + i]) else: break if (j + 1) * T - i < max_index: ans.append(s[(j + 1) * T - i]) else: break j += 1 i = i + 1 print i |