https://leetcode-cn.com/probl...解题思路由外向内移动,一次一个元素,所以空间复杂度为O(1).移动的顺序是从左上角开始,每次移动开始前,对应的四个位置都要向一侧旋转一次才结束,然后执行第二个位置。第一轮移动:1234第二轮移动:12-4寻找下一个位置的函数:defnext_xy(x,y,s):returny,s-1-x代码类解决方案:defrotate(self,matrix:List[List[int]])->None:"""不要返回任何东西,而是就地修改矩阵。"""defnext_xy(x,y,s):returny,s-1-xn=len(matrix)i=0foriinrange(n):l=n-i*2ifl<2:breakforjinrange(l-1):x,y=0,jt=matrix[x+i][y+i]forkinrange(4):nx,ny=next_xy(x,y,l)print(x,y,nx,ny)print(t,matrix[nx+i][ny+i])tt=matrix[nx+i][ny+i]matrix[nx+i][ny+i]=tx,y,t=nx,ny,tt欢迎来到我的博客:https://代码图。top/我的博客主题分类:https://codeplot.top/categories/%E5%88%B7%E9%A2%98/
