【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 ...
随机推荐
- 第二篇Scrum冲刺博客
第二篇Scrum冲刺博客 一.站立式会议 提供当天站立式会议照片一张 二.每个人的工作 成员 已完成工作 明天计划完成的工作 遇到的困难 林剑峰 初步学习小程序的编写.博客园的撰写 初步完成用户界面 ...
- python处理excel函数xlrd、xlwt
https://www.jianshu.com/p/f2c9dff344c6 https://www.cnblogs.com/ilovepython/p/11068841.html 行列操作:http ...
- debug模式不报错,release模式报错
经常会 char * pMem = new char[icount]; 其中icount为变量,然后对该内存段猛的操作.release编译出来,出现莫名奇妙的错误.但是debug没问题. 后面查了别人 ...
- Android 获取联系人的号码的类型描述
...... int index = phonesCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); int t ...
- 24-C#笔记-异常处理
# 1 语法 try catch finally(相当于catch(...)) using System; ... public void division(int num1, int num2) { ...
- 前端页面模拟浏览器搜索功能Ctrl+F实现
<html> <head> <style type="text/css"> .res { color: Red; } .result{ back ...
- 【oracle】处理锁表
查询锁表 select object_name,machine,s.sid,s.serial# from v$locked_object l,dba_objects o ,v$session swhe ...
- hadoop KerberosUtil 做Kerberos认证
网上找了一下,自己写了个KerberosUtil工具类,测试过可以用. 注意这个不是 org.apache.hadoop.security.authentication.util.KerberosUt ...
- OD(lfdnb)
由于一场意外,D死了,在此开一个新坑 2019.11.13 考前焦虑 智商为负 有点担心考试状态 2019.11.12 上午考试简直心态爆炸 T1看了一个小时不会 然后看T2,这时候wxy聚聚已经切了 ...
- 洛谷 P2615 神奇的幻方
传送门 I'm here! 思路 这个题,我们可以直接去模拟,因为范围很小,且\(N\)都是奇数 直接构造一个矩阵,初始值都为\(0\),然后\(while\)循环,根据题目给出的\(4\)个条件进行 ...