[一起來刷leetcode吧][31]--No.6 ZigZag Conversion

這篇文章是程序自動發表的,詳情可以見這裏
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
發佈了78 篇原創文章 · 獲贊 16 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章