天天看点

Leetcode:Rotate Image 旋转图片

you are given an n x n 2d matrix representing an image.

rotate the image by 90 degrees (clockwise).

follow up:

could you do this in-place?

刚开始没有思路,但是自己举了几个简单的栗子才看出规律:需要一圈儿一圈儿的变换位置。有两层for循环:外层控制圈儿数;内层控制交换的元素位置。每一次都有四个元素需要交换。代码如下: