[leetcode]48. 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 NOT allocate 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]
]
题意:
In-place 顺时针旋转90度
Solution1: Try a simulation by matrix [2X2], find the principle how to rotate
i.e:
Original:
1 2
4
Rotated:
1
4 2
Q: How do we get value 3 from (1,0) to (0,0) ?
A: flip matrix from up to down
Flip up down from original:
3
2
Q: How do we put values 1 and 4 to their own places?
A: flip matrix by diagonal
Flip matrix by diagonal:
3 1
4 2
code
/*
Time:O(n^2). We use nested 2 for loop.
Space: O(1). We only used constant extra space.
*/
class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
// flip matrix from up to down
for(int i = 0; i < n/2; i++){
for(int j = 0; j< n; j++){
swap(matrix, i, j, n-i-1, j);
}
}
//flip matrix by diagonal
for(int i = 0; i < n; i++){
for(int j = i+1; j < n; j++){
swap(matrix, i, j, j, i);
}
}
}
private void swap(int[][] matrix, int i, int j, int a, int b){
int tmp = matrix[i][j];
matrix[i][j] = matrix[a][b];
matrix[a][b] = tmp;
}
}
[leetcode]48. Rotate Image旋转图像的更多相关文章
- [LeetCode] 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 (旋转图像) 解题思路和方法
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
- LeetCode 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(2D图像旋转问题)
题目链接: https://leetcode.com/problems/rotate-image/?tab=Description Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...
- [leetcode 48] rotate image
1 题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwi ...
- LeetCode 48. Rotate Image My Submissions Question (矩阵旋转)
题目大意:给一个矩阵,将其按顺时针旋转90°. 题目分析:通法是先将矩阵转置,然后再反转每一行,或者是先反转每一列,然后再将其转置.I just want to say"It's amazi ...
- LeetCode 48. Rotate Image (C++)
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- 【刷题笔记】LeetCode 48. Rotate Image
题意 原地顺时针翻转一个 n*n 的矩阵 图解 下面例子中用 5*5 矩阵做示例,如下图,我们要把该矩阵顺时针翻转90度,并且不能使用另外的矩阵空间来暂存数据,而是原地改变矩阵中数值. 我的想法是这样 ...
随机推荐
- 无状态http协议上用户的身份认证
1.注册时可以使用手机短信验证码进行身份认证 2.用户每次请求不能每次都发送验证码,这时需要服务器给客户端颁发一个身份凭证(一般为一个唯一的随机数),用户每次请求时都携带身份凭证, 服务器会记录该身份 ...
- Java注解总结2
注解是Java元数据,可以理解成代码的标签,正确使用能极大的简化代码的编写逻辑,在各种框架代码中使用也越来越多. 一.注解的应用场景 生成doc文档: 编译器类型格式检查: 运行时处理如注入依赖等 二 ...
- C# 左右补零
//不够4位补零 public static string addZero(int val) { string str = val + ""; int strLen = str.L ...
- 十六、springcloud(二)Eureka集群
1.创建子工程spring-cloud-peer(jar) 2.创建application-peer1.properties,application-peer2.properties applicat ...
- asp.net core 2.0 后台定时自动执行任务
自己写一个类继承BackgroundService internal class RefreshService : BackgroundService { protected override asy ...
- 当VUE 遇到微信js api
先哭一会儿... history hash 1.如果使用history 方式,需要使用window.localtion.href="路径" 2.然后使用this.$router.p ...
- Python基础------运算符
运算符类型 算数运算符 + 加 - 减 * 乘 / 除 %取余 ...
- startup.bat 一闪而过解决方案
原文地址:http://blog.csdn.net/stypace/article/details/38083581 启动tomcat时cmd窗口一闪而过解决方法. 注: 我这边只是配置了环境变量就o ...
- enctype=“multipart/form-data”详解
enctype这个属性管理的是表单的MIME(Multipurpose Internet Mail Extensions)编码,共有三个值可选: 1.application/x-www-form-ur ...
- 四、Html列表、块、布局