LeetCode:旋转图像【48】
LeetCode:旋转图像【48】
题目描述
给定一个 n × n 的二维矩阵表示一个图像。
将图像顺时针旋转 90 度。
说明:
你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。
示例 1:
给定 matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
], 原地旋转输入矩阵,使其变为:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
示例 2:
给定 matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
], 原地旋转输入矩阵,使其变为:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]
题目分析
最笨的方法是模拟,可是在短时间内根本无法准确提取出模拟的所有边界判断条件,代码异常冗余,且无法进行优化。
然后怎么办呢?我们分析题目可以看出一个隐含条件,就是所给的矩阵一定是正方形的。因为我们不能新建数组,那么原有数组为长方形的话,无论如何我们都是无法表示结果的。
到这里然后呢?我们首先将其进行行列转置!

接着,我们再关于中心轴做镜像变换,swap(arr[i][j], arr[i][matrix.length-1-j])

最后我们应该思考一下为什么这样可以呢?
Java题解
public void rotate(int[][] matrix) {
for(int i = 0;i<matrix.length;i++) {
for (int j = i; j < matrix[0].length; j++) {
int temp = 0;
temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for(int i =0 ; i<matrix.length; i++){
for(int j = 0; j<matrix.length/2; j++){
int temp = 0;
temp = matrix[i][j];
matrix[i][j] = matrix[i][matrix.length-1-j];
matrix[i][matrix.length-1-j] = temp;
}
}
}
LeetCode:旋转图像【48】的更多相关文章
- LeetCode(48):旋转图像
Medium! 题目描述: 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转 ...
- Leetcode题目48.旋转图像(中等)
题目描述: 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1 ...
- [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:旋转图像
题目描述 给定一个 n × n 的二维矩阵 matrix 表示一个图像.请你将图像顺时针旋转 90 度. 你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要 使用另一个矩阵来旋转图 ...
- LeetCode第[48]题(Java):Rotate Image
题目:矩阵旋转 难度:Medium 题目内容: You are given an n x n 2D matrix representing an image. Rotate the image by ...
- 【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 48. Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【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 ...
随机推荐
- AJAX动态加载评论
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- knockout+weui+zepto
主文件wxapp.ts 1>在ts中,建议继承componentui //操作菜单表 actionsheet showactionsheet { title 上拉菜单标题 btns 操作组 建议 ...
- Struts1标签
Struts1 标签库 说明 Struts提供了五个标签库,即:HTML.Bean.Logic.Template和Nested. HTML 标签 : 用来创建能够和Struts 框架和其他相应的HT ...
- Spring MVC Hibernate验证器
下面的示例演示如何使用Spring Web MVC框架在表单中使用错误处理和验证器. 首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web ...
- PC如何访问手机网址
http://blog.csdn.net/matthew_fan/article/details/7787504
- 第二章----python函数
第一节:调用函数 1.函数是什么? 函数是组织好的,可以重复利用的. 2.为什么要用到函数? 提高应用的模块性,提高重复利用率.指的是:多个文件中可能都要用到该函数,直接拿来调用就行,不用在重复写一个 ...
- Lucene索引文件组成
Lucene的索引里面存了些什么,如何存放的,也即Lucene的索引文件格式,是读懂Lucene源代码的一把钥匙. 当我们真正进入到Lucene源代码之中的时候,我们会发现: Lucene的索引过程, ...
- php简单工厂模式
工厂类中有一个创建对象的方法,根据传入参数的不同来生成不同的对象 class Operation extends Model{ private $numberA; private $numberB; ...
- Android开发:《Gradle Recipes for Android》阅读笔记1.4
在android studio下可以使用gradle视图运行gradle. gradle任务被分别组织在不同目录下,如android,build,install. 需要执行特定的任务只要双击任务名称. ...
- OSPF-lsa-types