【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 ...
随机推荐
- 更改用户host留下的坑
前言: 我们在创建数据库用户的时候都会指定host,即一个完整的用户可描述为 'username'@'host' .创建用户时不显式指定host则默认为%,%代表所有ip段都可以使用这个用户,我们也 ...
- 部署ceph存储集群及块设备测试
集群环境 配置基础环境 添加ceph.repo wget -O /etc/yum.repos.d/ceph.repo https://raw.githubusercontent.com/aishang ...
- js实现时分秒毫秒计时器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 解决opencart设置SSL后评论不能翻页的问题
为了网站的安全和seo,我们为客户的opencart网站添加了SSL加密实现https,并设置了301跳转使http跳到https,基本所有的功能都完好,就是有一点评论分页无法加载分页,去分析了链接源 ...
- 阿里巴巴java开发手册 学习
3. [强制]类名使用 UpperCamelCase 风格,必须遵从驼峰形式,但以下情形例外: DO / BO / DTO / VO / AO 正例: MarcoPolo / UserDO / Xml ...
- 异常CLRDBG_NOTIFICATION_EXCEPTION_CODE( 0x04242420)的抛出过程
新建一个c#控制工程,就用自动生成的代码,不用补任何代码,如下: using System; using System.Collections.Generic; using System.Linq; ...
- 洛谷p3353在你窗外闪耀的星星题解
题目 首先被题目甜到了 本来搜标签搜的线段树,结果发现这题目很吸引我我果断点开 觉得前缀和就能A啊 于是乎 要注意 窗户旁边是可以看到的 所以前缀和的时候是不用再-1的 //前缀和 //注意坑点 // ...
- PATA1031 Hello World for U
参考代码: #include <cstdio> #include <cstring> int main() { char str[100], ans[40][40]; scan ...
- servlet中的doGet()与doPost()以及service()的用法
doget和dopost的区别 get和post是http协议的两种方法,另外还有head, delete等 1.这两种方法有本质的区别,get只有一个流,参数附加在url后,大小个数有严格限制且只能 ...
- Venn 维恩图的绘制
使用在线绘图工具 https://bioinfogp.cnb.csic.es/tools/venny/index.html http://www.biovenn.nl/index.php http:/ ...