[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度,并且不能使用另外的矩阵空间来暂存数据,而是原地改变矩阵中数值. 我的想法是这样 ...
随机推荐
- [转]ANTS Performance Profiler和ANTS Memory Profiler 使用
.NET性能调优之一:ANTS Performance Profiler的使用 .NET性能调优系列文章 系列文章索引 .NET性能调优之一:ANTS Performance Profiler的使 ...
- Python基础------运算符
运算符类型 算数运算符 + 加 - 减 * 乘 / 除 %取余 ...
- python3学习笔记五(列表2)
参考http://www.runoob.com/python3/python3-list.html 嵌套列表 a = ['a','b','c']b = [1,2,3]x = [a, b]print(x ...
- 二叉树遍历(flist)(已知中序和按层遍历,求先序 )
问题 F: 二叉树遍历(flist) 时间限制: 1 Sec 内存限制: 128 MB提交: 11 解决: 9[提交][状态][讨论版][命题人:quanxing][Edit] [TestData ...
- matplotlib绘图总结《转》
本文作为学习过程中对matplotlib一些常用知识点的整理,方便查找. 类MATLAB API 最简单的入门是从类 MATLAB API 开始,它被设计成兼容 MATLAB 绘图函数. from p ...
- k8s学习笔记之九: Service Account
第一章.前言 每一个用户对API资源进行操作都需要通经过以下三个步骤: 第一步:对客户端访问进行认证操作,确认是否具有访问k8s权限 token(共享秘钥) SSL(双向SSL认证) ....通过任何 ...
- python大法好——异常
---恢复内容开始--- Python 异常处理 python提供了两个非常重要的功能来处理python程序在运行中出现的异常和错误.你可以使用该功能来调试python程序. 异常处理: 本站Pyth ...
- java Integer类以及拆箱和装箱
package com.ilaw.boson.controller; public class Demo { public static void main(String[] args) { Inte ...
- Java遍历树(深度优先+广度优先)
在编程生活中,我们总会遇见树性结构,这几天刚好需要对树形结构操作,就记录下自己的操作方式以及过程.现在假设有一颗这样树,(是不是二叉树都没关系,原理都是一样的) 1.深度优先 英文缩写为DFS即Dep ...
- 2D射影空间,为何引入射影空间
2D欧氏空间R2中,点的表示是A(x1,y1), B(x2,y2),二维参数,线的表示是L: y=kx+b,是二维参数: 如何表示点在线上面?可以扩展为(k,-1,b)* (x1,y1,1)t = 0 ...