LeetCode OJ算法题(四十七):Rotate Image
来源:互联网 发布:潜在因子推荐算法 编辑:程序博客网 时间:2024/05/03 13:11
题目:
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度,观察可知是将所有的A[ i ][ j ]移动到A[ j ][ n-1-i ]的位置,但是考虑到要in-place,所有只能使用交换元素的方法。
可以首先将A[ i ][ j ]与A[ n-1-j ][ n-1-i ]交换位置,再把矩阵上下对调,即A[n-1-j][n-1-i]变换到A[j][n-1-i]上,这样只要保证不重复交换,就能保证矩阵正确旋转!
public static void rotate(int[][] matrix) {int n = matrix.length; for(int i=0;i<n;i++){ for(int j=0;j<n-i;j++){ int tmp = matrix[n-1-j][n-1-i]; matrix[n-1-j][n-1-i] = matrix[i][j]; matrix[i][j] = tmp; } } for(int i=0;i<=n/2-1;i++){ for(int j=0;j<n;j++){ int tmp = matrix[n-1-i][j]; matrix[n-1-i][j] = matrix[i][j]; matrix[i][j] = tmp; } } }
0 0
- LeetCode OJ算法题(四十七):Rotate Image
- LeetCode OJ:Rotate Image
- LeetCode OJ - Rotate Image
- [LeetCode OJ]Rotate Image
- LeetCode OJ Rotate Image
- LeetCode OJ算法题(四十):First Missing Positive
- LeetCode OJ算法题(四十三):Wildcard Matching
- <LeetCode OJ> 48. Rotate Image
- LeetCode OJ-48-Rotate Image
- leetcode:Rotate Image (旋转矩阵)【面试算法题】
- LeetCode算法题之Rotate Image
- LeetCode OJ算法题(六十):Rotate List
- LeetCode OJ 之 Rotate Image (旋转图像)
- Leetcode算法题目:Rotate Image
- LeetCode OJ——Rotate Image
- LeetCode 第 48 题(Rotate Image)
- LeetCode(48)Rotate Image
- 48. Rotate Image ---leetcode算法笔记
- 浅谈jquery中,html、val与text三者属性取值的联系与区别
- CocoaPods安装和使用教程
- LeetCode-Unique Binary Search Trees II
- Windows下,Eclipse的Android NDK(r8e) 配置
- hdu 1781 Friend
- LeetCode OJ算法题(四十七):Rotate Image
- 编写C语言的技巧
- 引用
- poj2255(二叉树重建)
- Using the NDK plugin
- 验证码处理
- #ifndef,#define,#endif和#pragma once
- JS日期时间选择器
- LINUX 网络爬虫中使用正则匹配URL