`

Leetcode - Rotate Image

 
阅读更多
[分析]
自己的思路:从外到内一圈圈顺时针旋转90度,坐标映射问题。
Leetcode讨论区有很多有趣巧妙的思路,列举两个点赞率较高思路:
1)上下颠倒,然后转置
/*
* clockwise rotate
* first reverse up to down, then swap the symmetry
* 1 2 3     7 8 9     7 4 1
* 4 5 6  => 4 5 6  => 8 5 2
* 7 8 9     1 2 3     9 6 3
*/
2)转置,然后左右颠倒
若需要逆时针,则分别是:
1)左右颠倒,然后转置
2)转置,然后上下颠倒
[ref]
https://leetcode.com/discuss/20589/a-common-method-to-rotate-the-image

public class Solution {
    public void rotate1(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
            return;
        int rowBeg = 0, rowEnd = matrix.length - 1;
        int colBeg = 0, colEnd = matrix[0].length - 1;
        while (rowBeg <= rowEnd) {
            int offset = colEnd - colBeg - 1;
            for (int j = 0; j <= offset; j++) {
                int save = matrix[rowBeg][colBeg + j];
                matrix[rowBeg][colBeg + j] = matrix[rowEnd - j][colBeg];
                matrix[rowEnd - j][colBeg] = matrix[rowEnd][colEnd - j];
                matrix[rowEnd][colEnd - j] = matrix[rowBeg + j][colEnd];
                matrix[rowBeg + j][colEnd] = save;
            }
            rowBeg++; rowEnd--;
            colBeg++; colEnd--;
        }
    }
    // first transpose, then flip the matrix horizontally
    public void rotate(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
            return;
        int rows = matrix.length, cols = matrix[0].length;
        for (int i = 0; i < rows; i++) {
            for (int j = i + 1; j < cols; j++) {
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = tmp;
            }
        }
        int half = cols / 2;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < half; j++) {
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[i][cols -1 - j];
                matrix[i][cols -1 - j] = tmp;
            }
        }
    }
}
分享到:
评论

相关推荐

    戳气球leetcode-leetcode:leetcode

    leetcode category other hot keywords:Palindrome(mic), Subsequence Array 螺旋矩阵Spiral Matrix 顺时针打印矩阵 Next Permutation Product of Array Except Self 189.rotate-array 283.move-zero Range Sum ...

    javalruleetcode-LeetCode::lollipop:个人LeetCode习题解答仓库-多语言

    java lru leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode 题解仓库,欢迎交流学习。 下面的目录中 ...Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73

    leetcode答案-LeetCode:LeetCode上的题解

    leetcode 答案 LeetCode 该项目是 LeetCode 上的题解。 src/easy/路径下都是难度为 ..._48_RotateImage这个类名。运行效果如下图所示: 把这个类名复制一下,新建类的时候直接把类名粘贴过去就可以了。

    LeetCode最全代码

    # [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...

    leetcode1004-leetcode:leetcode

    Rotate Image (M) -&gt; 2 73. Set Matrix Zeroes (M) 1. Two Sum (E) 167. Two Sum II - Input array is sorted (E) 653. Two Sum IV - Input is a BST (E) -&gt; 2 26. Remove Duplicates from Sorted Array (E) 27. ...

    leetcode2sumc-LeetCode_py:LeetCode_py

    RotateImage 153 - SpiralMatrix 顺时针旋转矩阵 90 度。 首先翻转矩阵,交换对称153是直接的 01/31/2020 75 - SortColors 167 - TwoSum2 DCP 75 是一个双指针问题,如果当前项为 0,则使用 p1 p2 指向开始和结束,...

    leetcode跳跃-leetcode:leetcode解题之路

    旋转图像](./Array/rotate-image.md) Heap 堆 [0023 合并K个排序链表](./Heap/merge-k-sorted-lists.md) String 字符串 [0006 Z字形变换](./String/zigzag-conversion.md) [0030 串联所有单词的子串](./String/...

    lrucacheleetcode-leetcode:记录自己的leetcode解题历程~Welcomeeveryonetocomment:grinning_face:~

    Rotate Image 344. Reverse String 414. Third Maximum Number 448. Find All Numbers Disappeared in an Array 66. Plus One 238. Product of Array Except Self 697. Degree of an Array 849. Maximize ...

    javalruleetcode-magician:java学习

    java lru leetcode ##Thinking in Java chapter21 ##Netty in Action ####chapter2: echo ...##leetcode ...[Rotate Image] () [Ugly Number] () [Ugly Number II] () [Repeated DNA Sequences] () [Lar

    cpp-算法精粹

    Rotate Image Plus One Climbing Stairs Set Matrix Zeroes Gas Station Candy Majority Element Rotate Array Contains Duplicate Contains Duplicate II Contains Duplicate III Product of Array Except Self ...

Global site tag (gtag.js) - Google Analytics