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?
题目:n x n的数组反转90度。
思路:举个例子观察一下就行了。第一行变成最后一列,第二行变成倒数第二列,以此类推。
public void rotate(int[][] nums) {
int len = nums[0].length;
List<Integer> list = new ArrayList<Integer>();
for(int i = 0; i < len;i++) {
for(int j = 0; j < len;j++){
list.add(nums[i][j]);
}
}
int k = 0;
for(int i = len -1; i>=0;i--){
for(int j = 0;j < len;j++) {
nums[j][i] = list.get(k++);
}
}
}
种一棵树最好的时间是十年前,其次是现在!