[LC] 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]
]
class Solution {
public void rotate(int[][] matrix) {
int len = matrix.length;
// rotate diagnal first
for (int i = 0; i < len; i++) {
for (int j = 0; j < i; j++) {
swap(matrix, i, j, j, i);
}
}
// rotate vertical next
for (int i = 0; i < len; i++) {
for (int j = 0; j < len/2; j++) {
swap(matrix, i, j, i, len - 1 - j);
}
}
}
private void swap(int[][] matrix, int a, int b, int c, int d) {
int tmp = matrix[a][b];
matrix[a][b] = matrix[c][d];
matrix[c][d] = tmp;
}
}
[LC] 48. Rotate Image的更多相关文章
- [Leetcode][Python]48: Rotate Image
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 48: Rotate Imagehttps://leetcode.com/pr ...
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
- 11/8 <matrix> LC 48 54 59
48. Rotate Image 先按对角线对称图形,再水平对折. class Solution { public void rotate(int[][] matrix) { //1.transpos ...
- 刷题48. Rotate Image
一.题目说明 题目是48. Rotate Image,简而言之就是矩阵顺时针旋转90度.不允许使用额外的矩阵. 经过观察(写一个矩阵,多看几遍就知道了),旋转90度后: 第1行变为len-1列(最后一 ...
- 48. Rotate Image - LeetCode
Question 48. Rotate Image Solution 把这个二维数组(矩阵)看成一个一个环,循环每个环,循环每条边,每个边上的点进行旋转 public void rotate(int[ ...
- [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
1 题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwi ...
- 48. Rotate Image
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- leetCode 48.Rotate Image (旋转图像) 解题思路和方法
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
随机推荐
- Python对象赋值、浅拷贝、深拷贝
Python中,基本数据类型,理解为常见数据类型:布尔型.整型.浮点型.字符串.列表.元组.字典.集合,随语言不同而不同,但是根据在内存中存储方式的不同,区分开原子类型和容器类型. 对象赋值 对象的赋 ...
- RFC文档(http部分)
Request For Comments(RFC),是一系列以编号排定的文件.文件收集了有关互联网相关信息,以及UNIX和互联网社区的软件文件.目前RFC文件是由Internet Society(IS ...
- 云托管,边缘物理计算&托管物理计算,你所需要了解的……
随着业务发展,传统数据中心建设复杂性越来越高,基建的管理.设备的繁杂.人力成本的提升,是否让你的运维成本越来越高?企业生产效率却越来越低? 业务快速发展,设备采购周期冗长,大量采购造成CAPEX过重, ...
- 吴裕雄--天生自然 PYTHON3开发学习:列表
list1 = ['Google', 'Runoob', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b& ...
- UML-如何进行面向对象设计?
1.开发者如何设计对象? 1).直接编码 2).uml图,然后编码 3).uml图,不编码 绘图要轻量的 2.并行创建若干模型 如:5分钟画交互图,5分钟画类图.反复交替 3.选择什么样的UML CA ...
- 箭头函数,闭包函数中的this指向
在javscript中,this 是在函数运行时自动生成的一个内部指针,它指向函数的调用者. 箭头函数有些不同,它的this是继承而来, 默认指向在定义它时所处的对象(宿主对象),而不是执行时的对象. ...
- Linux下切换用户出现su: Authentication failure的解决办法
在切换用户时,密码没有输错,但始终无法成功地切换,还报出身份验证失败的错误,下面是具体解决方案: 在终端上输入指令sudo passwd root 此时输入你的密码 重复再次输入你的密码 再次用su指 ...
- ant design for vue 关于table的一些问题
1.为table添加分页: :pagination="pagination" pagination: { defaultPageSize: 10, showTotal: (tota ...
- vue动态绑定class,tab栏
- ZJNU 1133 - Subset sequence——中级
推出n=1到4时,An排列的种类数分别为1 4 15 64可得(1+1)*2=4(4+1)*3=15(15+1)*4=64...故用一数列r[n]记录An的种类总数当n=3时,列举出以下15种从大到小 ...