LeetCode--059--螺旋矩阵 II(python)

 

效率超级低,但是能过。。。。

 1 class Solution:
 2     def generateMatrix(self, n):
 3         tR = tC = 0
 4         dR = n-1
 5         dC = n-1
 6         x = [[0 for i in range(n)] for j in range(n)]
 7         nowNum=1
 8         while(tR <= dR and tC <=dC):
 9             nowNum = self.draw(tR,tC,dR,dC,x,nowNum)
10             tR+=1
11             tC+=1
12             dR-=1
13             dC-=1
14         return x
15     def draw(self,tR,tC,dR,dC,lists,nowNum):
16         i = tC
17         j = tR
18         flag = False
19         while(i<=dC):#往右
20             lists[tR][i]=nowNum
21             #print("{} {} {}".format(tR,i,nowNum))
22             nowNum+=1
23             i+=1
24         i-=1
25         j+=1
26         while(j<=dR):#往下
27             lists[j][dC]=nowNum
28 #             print("{} {} {}".format(j,dC,nowNum))
29             nowNum+=1
30             j+=1
31         j-=1
32         i-=1
33         if j == dR and dR != tR:
34             while(i >= tC):#往左
35                 lists[dR][i]=nowNum
36 #                 print("{} {} {}".format(dR,i,nowNum))
37                 nowNum+=1
38                 flag = True
39                 i-=1
40             i+=1
41         
42         if flag and i==tC:
43             j-=1
44             while(j >tR):#往上
45                 lists[j][tC]=nowNum
46                 #print("{} {} {}".format(j,tC,nowNum))
47                 nowNum+=1
48                 j-=1
49             j+=1
50         return nowNum

 

上一篇:分布式事务 中间件 Seata学习 一


下一篇:如何建立TC的快捷方式!