您的位置:首页 > 其它

Roate image by 90 degree in place

2014-11-01 05:43 225 查看
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?

public class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
// let us trust that it is n by n
int start = 0, end = n-1;
while(start < end){
for(int i=start; i<end; i++){
int tmp = matrix[start][i];
matrix[start][i] = matrix[end+start-i][start];
matrix[end+start-i][start] = matrix[end][end+start-i];
matrix[end][end+start-i] = matrix[i][end];
matrix[i][end] = tmp;
}

start++;
end--;
}

}
}

easy problem.
time complexity is O(n^2)

space complexity  is O(1)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: