59. Spiral Matrix II
2022/2/4 6:12:32
本文主要是介绍59. Spiral Matrix II,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
This is the same problem with https://www.cnblogs.com/feiflytech/p/15862380.html
public int[][] generateMatrix(int n) { int[][] matrix = new int[n][n]; int top = 0, bottom = n - 1, left = 0, right = n - 1; int num = 1; int status = 0; while (top <= bottom && left <= right) { if (status % 4 == 0) { for (int j = left; j <= right; j++) { matrix[top][j] = num++; } top++; } else if (status % 4 == 1) { for (int i = top; i <= bottom; i++) { matrix[i][right] = num++; } right--; } else if (status % 4 == 2) { for (int j = right; j >= left; j--) { matrix[bottom][j] = num++; } bottom--; } else if (status % 4 == 3) { for (int i = bottom; i >= top; i--) { matrix[i][left] = num++; } left++; } status++; } return matrix; }
这篇关于59. Spiral Matrix II的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?