6 - leetcode ZigZag Conversion

来源:互联网 发布:网上开个淘宝店流程图 编辑:程序博客网 时间:2024/06/01 17:33

直观版

leetcode oj不过- -!有大神知道哪里错了的,求指导…..

#!/usr/bin/python# -*- coding: utf-8 -*-'''英文:ZigZag Conversion中文,举例:见http://blog.csdn.net/ljiabin/article/details/40477429'''class Solution(object):    def convert(self, s, numRows):        """        :type s: str        :type numRows: int        :rtype: str        """        length = len(s)        if not length or numRows == 1:            return s        a = []        for i in range(numRows):            a.append(length * [0])        row = col = 0        flag = True        for i in s:            a[row][col] = i            if flag:#竖直的往下走,列不变,行数增加                row += 1                if row == numRows: #超过最后一行后,改变方向变为斜向上。                    row = numRows - 2                    flag = False                    col = col + 1            else:#斜向上方向的数                row -= 1                col += 1                if row < 0:                    row = 1                    flag = True        s = ''        for i in range(numRows):            for j in range(length):                if a[i][j] != 0:                    s += a[i][j]        return sif __name__ == "__main__":    a = Solution()    print a.convert('0123456789',4)

规律版

见http://blog.csdn.net/ljiabin/article/details/40477429

0 0
原创粉丝点击