这篇文章是程序自动发表的,详情可以见
这里
href="http://ounix1xcw.bkt.clouddn.com/github.markdown.css" rel="stylesheet">
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