LeetCode1.1

作者: supermanwasd | 来源:发表于2019-01-01 18:31 被阅读0次

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%。

相关文章

  • LeetCode1.1

    ZigZag Conversion 今天题目难度中等 ZigZag Conversion 即为对角线结构。看出规律...

网友评论

    本文标题:LeetCode1.1

    本文链接:https://www.haomeiwen.com/subject/ewtolqtx.html