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++);
}
}
}种一棵树最好的时间是十年前,其次是现在!

本文介绍了一种实现二维数组顺时针旋转90度的方法,并提供了具体的Java代码实现。该方法首先将原数组的元素按行读取并存入列表中,再通过倒序的方式重新填充数组以完成旋转。
8万+

被折叠的 条评论
为什么被折叠?



