效率超级低,但是能过。。。。
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+=111 tC+=112 dR-=113 dC-=114 return x15 def draw(self,tR,tC,dR,dC,lists,nowNum):16 i = tC17 j = tR18 flag = False19 while(i<=dC):#往右20 lists[tR][i]=nowNum21 #print("{} {} {}".format(tR,i,nowNum))22 nowNum+=123 i+=124 i-=125 j+=126 while(j<=dR):#往下27 lists[j][dC]=nowNum28 # print("{} {} {}".format(j,dC,nowNum))29 nowNum+=130 j+=131 j-=132 i-=133 if j == dR and dR != tR:34 while(i >= tC):#往左35 lists[dR][i]=nowNum36 # print("{} {} {}".format(dR,i,nowNum))37 nowNum+=138 flag = True39 i-=140 i+=141 42 if flag and i==tC:43 j-=144 while(j >tR):#往上45 lists[j][tC]=nowNum46 #print("{} {} {}".format(j,tC,nowNum))47 nowNum+=148 j-=149 j+=150 return nowNum