LeetCode OJ 48. Rotate Image
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的更多相关文章
- [Leetcode][Python]48: Rotate Image
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 48: Rotate Imagehttps://leetcode.com/pr ...
- 【一天一道LeetCode】#48. Rotate Image
一天一道LeetCode系列 (一)题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 ...
- 【LeetCode】48. Rotate Image
Difficulty:medium More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/rotate-image/ ...
- 【LeetCode】48. Rotate Image 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 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 ...
- 【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 ...
- 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 ...
- LeetCode OJ:Rotate Image(旋转图片)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
随机推荐
- LeetCode 476. Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- Linux连接xshell找不到IP信息
虚拟机环境下的Linux连接xshell的网络连接找不到eth0(IP)信息的解决方法 1 输入ifconfig,如果有eth0信息,直接填写eth0上面的IP信息 2 输入ifconfig ...
- js 数值格式化函数
function ForDight(Dight,How){ ,How))/Math.pow(,How); return Dight; } //ForDight(Dight,How):数值格式化函数; ...
- python3 流程控制
表达式if ... else >>> if 3 > 4: ... print('False') ... else: ... print('True') ... True 表达式 ...
- apache bench(ab)压力测试模拟POSt请求
ab命令格式: -N|--count 总请求数,缺省 : 5w -C|--clients 并发数, 缺省 : 100 -R|--rounds 测试次数, 缺省 : 10 次 -S|-sleeptime ...
- VS2015转VS2008
对于Windows8及以上的系统,无法安装VS2008,也不是无法安装,如果玩游戏多的,应该已经安装了.NET3.5,可以正常安装. 既然不能安装VS2008,那就安装VS2015好了,现在已经是Up ...
- <hdu - 1280> 前M大的数 (注意其中的细节)
这是杭电hdu上的链接http://acm.hdu.edu.cn/showproblem.php?pid=1280 Problem Description: 还记得Gardon给小希布置的那个作业么 ...
- Scala 字段定义
Scala 中定义字段只有val, var两种方式,都要在定义的同时即赋值,var 可以用占位符' _ '代替. 抽象类中定义的抽象字段不用赋初值,抽象方法也不用写方法体.(在子类中给抽象字段赋值和实 ...
- CentOs + Nginx + php-fpm + MySql 依赖库安装
依赖库和开发工具 yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype free ...
- Learning from the CakePHP source code - Part I
最近开始痛定思痛,研究cakephp的源码. 成长的路上从来没有捷径,没有小聪明. 只有傻傻的努力,你才能听到到成长的声音. 下面这篇文章虽然过时了,但是还是可以看到作者的精神,仿佛与作者隔着时空的交 ...