题解 | #顺时针旋转矩阵#

顺时针旋转矩阵

http://www.nowcoder.com/practice/2e95333fbdd4451395066957e24909cc

顺时针旋转矩阵:本来是左上读到右下,现在顺时针旋转90°后,应该从左下读到右上。

以下是代码实现:
①定义一个二维数组
②写出两个for循环,从左下角从下往上的顺序开始读取元素
③注意:获取mat元素时i和j谁在行谁在列
④将获取的元素放入二维数组中,return即可。
public int[][] rotateMatrix(int[][] mat, int n) {
// write code here
int[][] res = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = n - 1; j > -1; j--) {
res[i][n - 1 - j] = mat[j][i];
}
}
return res;
}

全部评论

相关推荐

完美的潜伏者许愿简历...:隐藏信息被你提取出来了,暗示,这就是暗示
点赞 评论 收藏
分享
牛客38347925...:9,2学生暑期实习失利开始投小厂,给这群人整自信了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务