LeetCode:旋转图像【48】

题目描述

给定一个 × 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】的更多相关文章

  1. LeetCode(48):旋转图像

    Medium! 题目描述: 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转 ...

  2. Leetcode题目48.旋转图像(中等)

    题目描述: 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1 ...

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

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

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

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

  5. LeetCode:旋转图像

    题目描述 给定一个 n × n 的二维矩阵 matrix 表示一个图像.请你将图像顺时针旋转 90 度. 你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要 使用另一个矩阵来旋转图 ...

  6. LeetCode第[48]题(Java):Rotate Image

    题目:矩阵旋转 难度:Medium 题目内容: You are given an n x n 2D matrix representing an image. Rotate the image by ...

  7. 【LeetCode】48. Rotate Image

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

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

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

  9. LeetCode OJ 48. Rotate Image

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

  10. 【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 ...

随机推荐

  1. 初识Quartz(二)

    简单作业: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package quartz_pr ...

  2. Cap&#39;n Proto, FlatBuffers, and SBE

    转自:utm_source=tuicool">http://kentonv.github.io/capnproto/news/2014-06-17-capnproto-flatbuff ...

  3. Modification of UCT with Patterns in Monte-Carlo Go(论文阅读)

    摘要:用于解决多臂赌博机UCB1算法已经被扩展成了解决极大极小树搜索的UCT算法.我们开发了一套Monte-Carlo围棋程序,MoGo,这是第一个使用UCT算法实现的计算机围棋程序.我们解释了为了围 ...

  4. 几种垃圾回收GC概述

    垃圾回收机制 引用计数回收器(Reference Counting Collector) 原理是在每个对象内部维护一个整数值,叫做这个对象的引用计数,当对象被引用时引用计数加一,当对象不被引用时引用计 ...

  5. grails email 发送邮件插件

    1.配置email插件,在Config.groovy文件中配置: plugins { compile ":mail:1.0.5" } 2.配置Config.groovy文件: gr ...

  6. easyUI combox静态动态联动

    easyUI重写了select,取而代之的是combobox,有如下几种方式可以创建一个combobox 1.使用select标签,并加上class="easyui-combobox&quo ...

  7. java String去除两端的空格和空字符

    java中String有个trim()能够去掉一个字符串的前后空格.但是trim()只能去掉字符串中前后的半角空格,而无法去掉全角空格.去掉全角空格需要在trim()方法的基础上加上一些判断.Stri ...

  8. python django -7 Git与项目

    git的使用,主要包括: 本地仓库的命令 远程仓库的命令 项目需求.页面.模型类的设计,及页面的使用 Git简介 Git是目前世界上最先进的分布式版本控制系统 安装 sudo apt-get inst ...

  9. java的一些问题

    1. 判断是否是奇数: public static boolean isOdd(int i) { return i %2 != 0 ; } 2. System.out.println(2.0 - 1. ...

  10. linux命令之面试题1

    1.请解释下列10个shell命令的用途 top:是linux下常用的性能分析工具,能够实时的显示系统中各个进程的资源占用情况,类似于windows的资源管理器,查看系统的cpu,内存,运行时间,交互 ...