【LeetCode】48. Rotate Image
Difficulty:medium
More:【目录】LeetCode Java实现
Description
https://leetcode.com/problems/rotate-image/
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOTallocate another 2D matrix and do the rotation.
Example 1:
Given input matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
], rotate the input matrix in-place such that it becomes:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
Example 2:
Given input matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
], rotate the input matrix in-place such that it becomes:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]
Intuition
1. transpose the matrix
2. flip the matrix horizontally
1 2 3 1 4 7 7 4 1
4 5 6 => 2 5 8 => 8 5 2
7 8 9 3 6 9 9 6 3
Solution
public void rotate(int[][] a) {
int n = a[0].length;
//transpose the matrix
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
int temp = a[i][j];
a[i][j]=a[j][i];
a[j][i]=temp;
}
}
//flip the matrix horizontally
for(int i=0; i<n; i++){
for(int j=0; j<n/2; j++){
int temp = a[i][j];
a[i][j] = a[i][n-1-j];
a[i][n-1-j] = temp;
}
}
}
Complexity
Time complexity : O(N)
Space complexity : O(1)
What I've learned
1. When rotating a matrix, we can transpose the matrix firstly, and then find the law.
More:【目录】LeetCode Java实现
【LeetCode】48. Rotate Image的更多相关文章
- 【LeetCode】48. Rotate Image 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【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】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 【一天一道LeetCode】#48. Rotate Image
一天一道LeetCode系列 (一)题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 ...
- 【LeetCode】189. Rotate Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...
- 【LeetCode】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】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】048. Rotate Image
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- 【LEETCODE】48、数组分类,简单级别,题目:189,217,219,268,283,414
package y2019.Algorithm.array; import java.util.Arrays; import java.util.Stack; /** * @ClassName Rot ...
随机推荐
- Java 包扫描器
包扫描器 获取一个包下的所有类,然后使用默认的类加载器加载到内存中 public static List<Class<?>> scanByPackage(String pack ...
- LeetCode 641. Design Circular Deque
原题链接在这里:https://leetcode.com/problems/design-circular-deque/ 题目: Design your implementation of the c ...
- 模拟赛T2 线段树优化建图+tarjan+拓扑排序
然而这只是 70pts 的部分分,考场上没想到满分怎么做(现在也不会) code: #include <cstdio> #include <string> #include & ...
- redhat quay 安装试用
最近redhat 开源了quay 容器镜像管理平台,参考官方文档跑的时候需要订阅,各种不好使,然后就自己基于源码构建了 一个镜像(使用官方的dockerfile,构建出来的太大了1.9G 以及push ...
- rpm 子包创建学习
rpm 在打包的时候,可以创建子包,这样可以清晰的进行软件包的拆分,以下是结合官方文档学习 的一个实践 预备条件 需要安装rpmdevtools spec 文件 内容 Name: foo Versio ...
- 笔记&想要置顶但懒得置顶的文章目录
1.\(\text{Markdown&Latex}\)学习笔记 2.一些笔记 3.好东西 4.线段树学习笔记 5.并查集学习笔记 6.友链 7.语录
- C语言博客作业4——数组
0.展示PTA总分 一维数组 二维数组 字符数组 1.本章学习总结 1.1学习内容总结 一维数组知识点: 无论是对一维数组还是二维数组进行应用时,我们对其下标的应用十分广泛. 1:一维数组的输入和遍历 ...
- 关于m3u8格式的视频文件ts转mp4下载和key加密问题
一,利用网站浏览器F12键,利用谷歌浏览器插件找到视频的.m3u8文件,并打开. 二,打开m3u8文件后,里面有很多.ts的链接,和key的链接. 三,保存为html文件,下载ts文件,代码如下:可加 ...
- R地图包 maps
安装 maps install.packages(“maps”) 使用 maps library("maps") nz <- map_data("nz")
- 在Linux上使用mssql-conf工具配置SQL Server 2017
mssql-conf是在Linux上安装SQL Server 2017后的一个配置脚本.你可以使用这个实用工具设置以下参数: Agent 启用SQL Server代理 Collation 设置一个新的 ...