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. 两句话帮你彻底记住gdb之eXamining memory

    对于刚学习Unix/Linux环境C编程的小朋友们或者写了很多所谓的C代码的老手们(其实很可能是机械程序员或者是伪程序员)来说,要记住gdb的eXaming memory的语法其实是相当不容易的,如果 ...

  2. openSuse使用技巧

    1.opensuse的gnome默认使用nautilus作为文件浏览工具,若要设置文件的默认排序和视图,参考网页 https://thelinuxexperiment.com/change-the-d ...

  3. jquery 获取当前对象的id取巧验证的一种方法

    <!doctype html><html><head><meta charset="utf-8"><title>titl ...

  4. How I Mathematician Wonder What You Are!(poj 3130)

    题意:求问多边形的核(能够看到所有点的点)是否存在. /* 对于这样的题目,我只能面向std编程了,然而还是不理解. 算法可参考:http://www.cnblogs.com/huangxf/p/40 ...

  5. 驱动相关Error

    驱动中 fltKernel.h报 EPROCESS和PETHREAD重定义异常解决办法 驱动编写中经常会莫名出现 error C2371: 'PEPROCESS' : redefinition; di ...

  6. apache bench(ab)压力测试模拟POSt请求

    ab命令格式: -N|--count 总请求数,缺省 : 5w -C|--clients 并发数, 缺省 : 100 -R|--rounds 测试次数, 缺省 : 10 次 -S|-sleeptime ...

  7. HTML中<title>与<h1>区别

    1)<title>标签表示的标题是整个网页的名字,即在浏览器顶部的tab栏里显示的.搜索引擎通过它来搜索网页:<title>标签里的文本不出现在页面内容里面. <h1&g ...

  8. Java NIO Path接口和Files类配合操作文件

    Java NIO Path接口和Files类配合操作文件 @author ixenos Path接口 1.Path表示的是一个目录名序列,其后还可以跟着一个文件名,路径中第一个部件是根部件时就是绝对路 ...

  9. [Q]注册申请码获取工具问题

    问题一: 异常信息: 有关调用实时(JIT)调试而不是此对话框的详细信息, 请参见此消息的结尾. ************** 异常文本 ************** System.Reflectio ...

  10. TDD(测试驱动开发)的推广方法论