48. Rotate Image

复制代码
/**
48. Rotate Image
https://leetcode.com/problems/rotate-image/description/
*/
class Solution {
    public void rotate(int[][] matrix) {
        //N x N matrix have floor(N/2) square cycers, for example 4x4 matrix have 2 cycles
        if (matrix==null || matrix[0].length==0){
            return;
        }
        int n = matrix.length;
        for (int x = 0;x<n/2;x++){
            for (int y = x;y<n-x-1;y++){
                int temp = matrix[x][y];
                //move value from right to top
                matrix[x][y] = matrix[n-1-y][x];
                // move values from bottom to right 
                 matrix[n-1-y][x]= matrix[n-1-x][n-1-y];
                  // move values from left to bottom 
                matrix[n-1-x][n-1-y] = matrix[y][n-1-x];
                //assign temp to left
                matrix[y][n-1-x] = temp;
            }
        }
    }
}

//kotlin


class Solution {
fun rotate(matrix: Array<IntArray>): Unit {
if (matrix.isEmpty() || matrix[0].isEmpty())
return
val n = matrix.size
for (i in 0..(n / 2 - 1)) {
for (j in i..n - 2 - i) {
val temp = matrix[i][j]
println("temp:" + temp)
println("matrix[n-1-j][i]:" + matrix[n - 1 - j][i])
matrix[i][j] = matrix[n - 1 - j][i]
matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j]
matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i]
matrix[j][n - 1 - i] = temp
}
}
}
}
 
复制代码

 

posted @   johnny_zhao  阅读(186)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示