`
SunnyYoona
  • 浏览: 365138 次
社区版块
存档分类
最新评论

[LeetCode]48.Rotate Image

 
阅读更多

【题目】

You are given annxn2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:
Could you do this in-place?

【题意】

给定一个n*n个2维矩阵来表示一个图。在原矩阵上旋转图形90°。

【分析】

思路1:

A[0][0] -> A[0][3]A[1][0] -> A[0][2]

A[0][1] -> A[1][3]A[2][0] -> A[0][1]

A[0][2] -> A[2][3]A[3][0] -> A[0][0]

A[0][3] -> A[3][3]

由此可得:对于n * n 的2维矩阵

A[i][j] -> A[j][n-1-i]


思路2:

纯模拟,从外到内一圈一圈的转,但这个方法太慢。

A[i][j] -> A[j][n-1-i] -> A[n-1-i][n-1-j] -> A[n-1-j][i] -> A[i][j]


思路3:

【代码1】

/*********************************
*   日期:2014-01-21
*   作者:SJF0115
*   题号: Rotate Image
*   来源:http://oj.leetcode.com/problems/rotate-image/
*   结果:AC
*   来源:LeetCode
*   总结:
**********************************/
#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <vector>
using namespace std;

class Solution {
public:
    void rotate(vector<vector<int> > &matrix) {
        int i,j;
        int n = matrix.size();
        vector<vector<int> >tempMatrix = matrix;
        for(i = 0;i < n;i++){
            for(j = 0;j < n;j++){
                tempMatrix[j][n-1-i] = matrix[i][j];
            }//for
        }//for
        for(i = 0;i < n;i++){
            for(j = 0;j < n;j++){
                matrix[i][j] = tempMatrix[i][j];
            }//for
        }//for
    }
};
int main() {
    Solution solution;
    vector<int> row1 = {1,2,3};
    vector<int> row2 = {4,5,6};
    vector<int> row3 = {7,8,9};
    vector<vector<int>> matrix;
    matrix.push_back(row1);
    matrix.push_back(row2);
    matrix.push_back(row3);
    solution.rotate(matrix);
    int n = matrix.size();
    for(int i = 0;i < n;i++){
        for(int j = 0;j < n;j++){
            printf("%d ",matrix[i][j]);
        }//for
        printf("\n");
    }//for
    return 0;
}



原地旋转,不能使用额外的空间存储矩阵。虽然本代码能AC,但是不符合题意。

【代码2】

class Solution {
public:
    void rotate(vector<vector<int> > &matrix) {
        int i,j,temp;
        int n=matrix.size();
        for(i = 0;i < n/2;++i) {
            for (j = i;j < n-1-i;++j) {
                temp = matrix[j][n-i-1];
                matrix[j][n-i-1] = 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] = temp;
            }//for
        }//for
    }
};

【代码3】

class Solution {
public:
    void rotate(vector<vector<int> > &matrix) {
        int i,j,temp;
        int n=matrix.size();
        // 沿着副对角线反转
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n - i; ++j) {
                temp = matrix[i][j];
                matrix[i][j] = matrix[n - 1 - j][n - 1 - i];
                matrix[n - 1 - j][n - 1 - i] = temp;
            }
        }
        // 沿着水平中线反转
        for (int i = 0; i < n / 2; ++i){
            for (int j = 0; j < n; ++j) {
                temp = matrix[i][j];
                matrix[i][j] = matrix[n - 1 - i][j];
                matrix[n - 1 - i][j] = temp;
            }
        }
    }
};






分享到:
评论

相关推荐

    LeetCode最全代码

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

    leetcode答案-LeetCode:LeetCode上的题解

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

    leetcode1004-leetcode:leetcode

    48. 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....

    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 ...

    戳气球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解题之路

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

    leetcode2sumc-LeetCode_py:LeetCode_py

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

    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