题解 | #向左移动牛群II#
向左移动牛群II
https://www.nowcoder.com/practice/2467ddd80a2942abbaa752f3c874dd79
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param n int整型
* @param k int整型
* @return int整型二维数组
*/
public int[][] rotateII (int n, int k) {
// write code here
int[][] result = new int[n][n];
int tmp = 1;
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
result[i][j] = tmp++;
}
}
while(k-- > 0){
int[][] temp = new int[n][n];
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
temp[i - 1 < 0?n -1:i-1][j -1 < 0?n-1:j -1] =result[i][j];
}
}
result = temp;
}
return result;
}
}


MDPI公司福利 433人发布