Rotate Image (Java)
来源:互联网 发布:局域网网络直播 编辑:程序博客网 时间:2024/05/22 08:25
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
顺时针旋转90度就相当于先按对角线转置,再行内转置(第一个和最后一个换,第二个和倒数第二个换。。。)。
Source
public class Solution { public void rotate(int[][] matrix) { int n = matrix.length; if(n == 0) return; for(int i = 0; i < n ; i++){ for(int j = i; j < n; j++){//转一半,一个上三角或者下三角,全转等于原矩阵 int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } for(int i = 0; i < n; i++){ for(int j = 0; j < n / 2; j++){ int temp = matrix[i][j]; matrix[i][j] = matrix[i][n - 1 - j]; matrix[i][n - 1 - j] = temp; } } }}
Test
public static void main(String[] args){ int[][] m = {{0,0,0,5},{4,3,1,4},{0,1,1,4},{1,2,1,3}}; new Solution().rotate(m);; for(int i = 0; i < m.length; i++){ for(int j = 0; j < m[0].length; j++){ System.out.print(m[i][j] + " "); //打空格注意用双引号,用单引号容易转换为char相加后的数值 } } }
0 0
- [Leetcode] Rotate Image (Java)
- Rotate Image Java
- Rotate Image (Java)
- [LeetCode][Java] Rotate Image
- [letecode java] Rotate Image
- (java)Rotate Image
- leetcode:Rotate Image 【Java】
- LeetCode – Rotate Image (Java)
- [leetcode-48]Rotate Image(java)
- leetcode-java-48. Rotate Image
- (Java)LeetCode-48. Rotate Image
- [LeetCode-Java]48. Rotate Image
- [LeetCode] 48. Rotate Image java
- [leetcode]48. Rotate Image(Java)
- [3]48. Rotate Image(Java)
- Rotate Image
- Rotate Image
- Rotate Image
- CentOS系统下单机版Hadoop环境配置(CentOS 7)
- Linux驱动头文件说明
- WinCE系统USB功能定制
- 4Sum
- win 7 64位操作系统安装orcle
- Rotate Image (Java)
- 软件测试基础——软件测试分类
- 关于android运行项目The connection to adb is down, and a severe error has occured。让我们重启eclipse问题
- Hibernate中对增删改查的小结
- 文件及文件夹的相关操作
- android基础1
- JBPM与tomcat的整合
- log4j与commons-logging,slf4j的关系
- Android输入框切换密码显示与隐藏