[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 ...
随机推荐
- pandas中DataFrame重置设置索引
在pandas中,经常对数据进行处理 而导致数据索引顺序混乱,从而影响数据读取.插入等. 小笔总结了以下几种重置索引的方法: import pandas as pd import numpy as n ...
- js根据当前日期 求一个月前 半年前 一年前的日期
function p(s) { return s < 10 ? '0' + s: s;}getlastmonth() function getlastmonth() { va ...
- nginx 负载策略
1.轮询(weight=1) 默认选项,当weight不指定时,各服务器weight相同,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. upstream bak ...
- Windows环境下spyder调用Arcpy
用python写代码还是喜欢spyder,所以在网上找了通过spyder调用arcpy的方法. 这篇帖子总结的方法非常详细,且通过本人实践,切实可行https://blog.csdn.net/qq_2 ...
- matlab初级
命令 ======== 系统命令 命令 功能 例 date 显示当前日期 ans = 20-Jul-2019 what 当前文件夹下的matlab文件 type 文件中的内容 type CV.m ...
- ArrayList集合存储VO封装对象后调用的问题
VO代码: public class VO4Blog { private int b_id; private int b_typeid; private String b_title; private ...
- java线程——notify通知的泄露
版权声明:本文为CSDN博主「兰亭风雨」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/ns_code/ar ...
- linux中awk的应用
1.awk的基本认识和使用方法,参考下面链接 https://www.cnblogs.com/timxgb/p/4658631.html 2.awk中关于条件判断的用法,如 https://blog. ...
- swoole使用内存
//swoole直接操作系统的内存 单线程每秒可执行三百万次 主要用于进程间的数据通信 $swoole_table = new swoole_table(1024);//1024为内创建内存对象所能存 ...
- Ubuntu16.04中Mysql 5.7 安装配置
记录在Ubuntu 16.04安装Mysql 5.7时遇到的一些问题. Mysql安装 使用如下命令进行安装: 1 sudo apt-get install mysql-server mysql-cl ...