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?


【题目解析】

这个题目的要求是把一个二维数组向右旋转九十度,就地实现。


【思路1】

相比较矩阵的转置,矩阵的旋转有什么不同呢?看下面的例子:

1,2,3          1,3,2      1,2,3          2,3,1

3,2,1    ----转置--->   2,2,3  3,2,1    ----旋转--->   3,2,2

2,3,1          3,1,1  2,3,1          1,1,3

观察上面的结果我们发现这两个不同操作的结果是不同的,但是又有一定的关系,即转置后的每一行是旋转后每一行的逆序。如果就地实现旋转的话,我们发现这是有一定难度的——最后一列变为第一行,倒数第二行变为第二列······,如果我们先转置再逆序的话是很容易实现的。


【思路2】


【java代码1】

 public class Solution {
public void rotate(int[][] matrix) {
int row = matrix.length;
if(row == 0) return;
int col = matrix[0].length;
if(col < row) return; for(int i = 0; i < row; i++){
for(int j = i + 1; j < row; j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
for(int k = 0, m = row - 1; k < m; k++, m--){
int temp = matrix[i][k];
matrix[i][k] = matrix[i][m];
matrix[i][m] = temp;
}
}
}
}

【代码2】

 class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
int i,j,temp;
int n=matrix.size();
for(i = ;i < n/;++i) {
for (j = i;j < n--i;++j) {
temp = matrix[j][n-i-];
matrix[j][n-i-] = matrix[i][j];
matrix[i][j] = matrix[n-j-][i];
matrix[n-j-][i] = matrix[n-i-][n-j-];
matrix[n-i-][n-j-] = temp;
}//for
}//for
}
};

LeetCode OJ 48. Rotate Image的更多相关文章

  1. [Leetcode][Python]48: Rotate Image

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 48: Rotate Imagehttps://leetcode.com/pr ...

  2. 【一天一道LeetCode】#48. Rotate Image

    一天一道LeetCode系列 (一)题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 ...

  3. 【LeetCode】48. Rotate Image

    Difficulty:medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/rotate-image/ ...

  4. 【LeetCode】48. Rotate Image 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. LeetCode OJ 189. Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  6. LeetCode OJ 61. Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  7. 【LeetCode】48. Rotate Image (2 solutions)

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

  8. LeetCode OJ:Rotate List(旋转链表)

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  9. LeetCode OJ:Rotate Image(旋转图片)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

随机推荐

  1. Jquery 操作DropDownList 根据条件选中

    $("#<%=DDLCounty.ClientID%> option").each(function () { if ($(this).text() == $(&quo ...

  2. [学习笔记]agileone安装登陆报错,localhost重定向次数过多

    1.找到.htaccess文件 2.修改RewriteEngine  参数为off即可

  3. Objective-C Runtime 运行时之六:拾遗(转载)

    前面几篇基本介绍了runtime中的大部分功能,包括对类与对象.成员变量与属性.方法与消息.分类与协议的处理.runtime大部分的功能都是围绕这几点来实现的. 本章的内容并不算重点,主要针对前文中对 ...

  4. OpenGL编译问题随手记

    1.error C2381: "exit" : 重定义:__declspec(noreturn) 不同 编译OpenGL   Red   Book   的例子时出现错误, stdl ...

  5. mac相关

    1 mac的动态链接库 mac基于bsd,所以它使用的是bsd风格的动态链接库,即以.dylib为后缀的动态链接库. 这个和windows的.dll和linux的.so相区分.

  6. Number Sequence (HDU 1711)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. Proteus中MATRIX-8X8 LED灯的连接

    上面8个引脚用于选择行,低电平有效.下面8个引脚用于选择列,高电平有效. 经测试,红色点阵LED与之相反,是上面的引脚用于选择列,且高电平有效:下面的引脚用于选择行,低电平有效. 在AT89C51单片 ...

  8. eclipse中link方式安装插件

    今天需要给eclipse安装svn插件,觉得link方式便于管理于是就打算用这种方式来安装. 我电脑上的eclipse的安装目录是   E:\tools\eclipse   下面开始安装 1.在ecl ...

  9. windows下寻找端口

    netstat -aon|findstr "5037" 找到占用5037的进程号: 根据进程号杀死进程 taskkill /pid 5136 /f tasklist|findstr ...

  10. 关于手动关闭BootStrap模态框

    在网上找手动关闭BootStrap模态框的解决方法,说是(需要引用bootstrap.js等): $("#myModal").modal('hide'); 但是我发现我的只能关闭 ...