程序员LeetCode交流

LeetCode1.1

2019-01-01  本文已影响0人  supermanwasd

ZigZag Conversion

Screen Shot 2019-01-01 at 6.19.30 PM.png

今天题目难度中等

ZigZag Conversion 即为对角线结构。
看出规律为,新序列字数永远为2n-2,这样就可以代码了。

答案:

   class Solution:
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if numRows==1: return s
        tmp=['' for i in range(numRows)]
        index=-1; step=1
        for i in range(len(s)):
            index+=step
            if index==numRows:
                index-=2; step=-1
            elif index==-1:
                index=1; step=1
            tmp[index]+=s[i]
        return ''.join(tmp)

这个答案的速度超过所有python代码的55.91%。

上一篇 下一篇

猜你喜欢

热点阅读