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
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)