您的位置:首页 > 其它

LeetCode 105 Rotate Image

2014-10-30 09:20 274 查看
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?

分析:

维持一个layer变量记录当期层,对于每一层,

四条边上相同位置元素循环赋值,这样只要维持一个临时变量即可。

public class Solution {
public void rotate(int[][] matrix) {
if(matrix == null || matrix.length <=1)
return;
int n = matrix.length;
//一层一层旋转
for(int layer=0; layer<n/2; layer++){
for(int i=layer; i<n-layer-1; i++){
//四条边上同一位置元素循环赋值
int temp = matrix[i][layer];
matrix[i][layer] = matrix[n-layer-1][i];
matrix[n-layer-1][i] = matrix[n-i-1][n-layer-1];
matrix[n-i-1][n-layer-1] = matrix[layer][n-i-1];
matrix[layer][n-i-1] = temp;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LeetCode Rotate Image