59. Spiral Matrix II——python 实现——array

59. Spiral Matrix II——python 实现——array_第1张图片

 

class Solution(object):
    def kthSmallest(self, n):
        b = []
        for i in range(n):
            a = []
            for j in range(n):
                a.append(0)
            b.append(a)
        total = n*n
        num = 1
        rowbegin = 0
        rowend = n
        colbegin = 0
        colend = n
        while num <= total:
            for j in range(colbegin, colend):
                b[rowbegin][j] = num
                num += 1
            rowbegin += 1    # 需要注意,重点
            for j in range(rowbegin, rowend):
                b[j][colend-1] = num
                num += 1
            colend -= 1
            for j in range(colend-1, colbegin-1, -1):
                b[rowend-1][j] = num
                num += 1
            rowend -= 1
            for j in range(rowend-1, rowbegin-1, -1):
                b[j][colbegin] = num
                num += 1
            colbegin += 1
        return b

你可能感兴趣的:(leetcode)