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. angular-ui-bootstrap插件API - Tabs

    Tabs 案例 <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> ...

  2. 将stack翻译成"堆栈"实在是误人子弟

    也不知道从何时起,也不知道是哪个"教授"还是"老师",将stack翻译成堆栈(据说台湾叫做"堆叠").窃以为,这种翻译实在是误人子弟(题外话 ...

  3. Html5浏览器端less应用

    之前的一个布局是用rem来做的 我上一段代码 div { margin: 0.833333333rem 0; } /* 去处a标签的下划线*/ a { text-decoration: none; } ...

  4. iOS程序崩溃相关的处理办法

    一.bug追踪 1.捕获异常:Exception breakpoint 步骤: 2.终止调用:Symbolic breakpoint 步骤:前两步和一 基本是一样的,不截图了,只是在第二步选择的时候选 ...

  5. xaml中的依赖属性

    wpf使用依赖属性完成数据绑定.动画.属性变更通知.样式化等.对于数据绑定.绑定到.NET属性源上的UI元素的属性必须是依赖属性 .net的一般属性定义如下 private int val;      ...

  6. Gentoo双网卡同时启用上内外网

    引言:本文配置网络通过 OpenRC/netifrc 方法(net.*scritps)配置. 外网网卡:enp3s4 内网网卡:enp2s0 外网地址(通过路由器) IP: 192.168.1.10 ...

  7. div显示与隐藏及height()函数

    总结与网络 1. $("#id").show()表示display:block,$("#id").hide()表示display:none; $("# ...

  8. Conversion to Dalvik format failed with error 1(android)

    1.如果不修改android sdk版本,则使用project clean 命令作用于某工程即可. (该处理方式只是在高版本中兼容了低版本工程,未真正意义上的升级) 2.如果修改android sdk ...

  9. dotnet的移除-->安装 升级

    之所以要升级dotnet 是因为项目中报了如下错误 意思就是版本低了 为了升级 我先要把之前的移除掉(用脚本移除) #!/usr/bin/env bash # # Copyright (c) .NET ...

  10. 整理了一份React-Native学习指南

    原文:  http://www.w3ctech.com/topic/909 自己在学习React-Native过程中整理的一份学习指南,包含 教程.开源app和资源网站等,还在不断更新中.欢迎pull ...