Leetcode - Rotate Image

Paste_Image.png

My code:

public class Solution {
    public void rotate(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
            return;
        int lengthSide = matrix.length;
        int count = lengthSide / 2;
        for (int i = 0; i < count; i++)
            rotate(0 + i, lengthSide, matrix);
        
    }
    
    private void rotate(int base, int lengthSide, int[][] matrix) {
        
        int count = lengthSide - 2 * base - 1;
        for (int i = 0; i < count; i++) {
            int x = base;
            int y = base + i;
            int temp = matrix[x][y];
            for (int j = 0; j < 4; j++) {
                int x1 = y;
                int y1 = lengthSide - 1 - x;
                int temp1 = matrix[x1][y1];
                matrix[x1][y1] = temp;
                temp = temp1;
                x = x1;
                y = y1;
            }
        }   
    }
}

My test result:

Paste_Image.png

这道题目不难吧。主要一个问题,你能否in place的完成旋转。
其实是可以的哈,也是我自己想出来的办法。
我发现,正方形是一圈圈正方形层层包围起来的。旋转的时候,每一层之间是互不干扰的。
所以,我先求出正方形层数,然后循环一下,对每一层进行相同的处理,用同一个方法,即可。
然后就是坐标转换。假设是 4 * 4 的正方形。
(0, 0) -> (0, 3) -> (3, 3) -> (3, 0)
假设边长是 s,则转换规则是,
(x, y) -> (y, s - x) -> (s - x, s - y) -> (s - y, x)
即xy坐标互换后,再用边长减去纵坐标得到此刻的纵坐标。
然后就是使用一些临时变量来帮助完成整个,四个单元的旋转过程。

**
总结: Array
**

Anyway, Good luck, Richardo!

My code:

public class Solution {
    public void rotate(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
            return;
        int n = matrix.length;
        for (int i = 0; i < (n + 1) / 2; i++) {
            for (int j = i; j < n - i - 1; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[n - j - 1][i];
                matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
                matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
                matrix[j][n - i - 1] = temp;
            }
        }
    }
}

这道题木就是简单的总结规律,找出映射关系,就行。
因为太累了,就懒得找了。网上找了下。然后自己把代码写了出来。
这样不好。
参考地址:
http://www.programcreek.com/2013/01/leetcode-rotate-image-java/

Anyway, Good luck, Richardo!

My code:

public class Solution {
    public void rotate(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return;
        }
        
        // swap top with bottom
        int top = 0;
        int bottom = matrix.length - 1;
        while (top < bottom) {
            for (int i = 0; i < matrix[0].length; i++) {
                int temp = matrix[top][i];
                matrix[top][i] = matrix[bottom][i];
                matrix[bottom][i] = temp;
            }
            top++;
            bottom--;
        }
        
        for (int i = 0; i < matrix.length; i++) {
            for (int j = i + 1; j < matrix[0].length; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
    }
}

其实还有更直接的做法。
顺时针:
先将矩阵上下全部颠倒, 必须全部颠倒。
然后,swap symmetry

逆时针的话,
先将矩阵左右全部颠倒,然后swap symmetry

参考网址:
https://discuss.leetcode.com/topic/6796/a-common-method-to-rotate-the-image

Anyway, Good luck, Richardo! -- 08/11/2016

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容