href="http://ounix1xcw.bkt.clouddn.com/github.markdown.css" rel="stylesheet">
這是leetcode的第6題--ZigZag Conversion
題目
The string "asdfghjklqw" 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) 原題目的例子有點難懂,我就自己舉個例子吧 num > = 4
a j s h k d g l w f q return ajshkdglwfq
思路 可以發現,第一行和最後一行,字母依次在s中相差一個週期,而其他行,有兩個點開始,同樣相差一個週期。
show me the code
class Solution(object): def convert(self, s, numRows): """ :type s: str :type numRows: int :rtype: str """ rst = "" l = len(s) if numRows == 1: return s delta = 2 * numRows -2 #period for i in range(numRows): index = i index2 = delta - i while index < l : rst = s[index] index = delta if 0 < i < numRows - 1 and index2 < l: rst = s[index2] index2 = delta return rst