leetCode-Spiral Matrix II
来源:互联网 发布:填表软件 编辑:程序博客网 时间:2024/05/19 17:08
Description:
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3,
You should return the following matrix:
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]
Solution:
class Solution { public int[][] generateMatrix(int n) { int result[][] = new int[n][n]; int right = n - 1,down = n - 1; int x = 0,y = 0; int count = 1; while(y <= right){ for(int i = y;i <= right;i++){ result[x][i] = count++; } x++; for(int j = x;j <= down;j++){ result[j][right] = count++; } right--; for(int i = right;i >= y;i--){ result[down][i] = count++; } down--; for(int j = down;j >= x;j--){ result[j][y] = count++; } y++; } return result; }}
阅读全文
0 0
- LeetCode: Spiral Matrix II
- LeetCode Spiral Matrix II
- [Leetcode] Spiral Matrix II
- LeetCode : Spiral Matrix II
- [LeetCode] Spiral Matrix II
- [LeetCode]Spiral Matrix II
- [leetcode]Spiral Matrix II
- LeetCode-Spiral Matrix II
- [leetcode] Spiral Matrix II
- LeetCode - Spiral Matrix II
- Leetcode: Spiral Matrix II
- 【LeetCode】Spiral Matrix II
- Leetcode Spiral Matrix II
- [LeetCode] Spiral Matrix II
- LeetCode | Spiral Matrix II
- LeetCode Spiral Matrix II
- Leetcode: Spiral Matrix II
- Spiral Matrix II -- LeetCode
- 巴什博奕,威佐夫博奕,尼姆博奕,斐波那契博弈模板
- 排序算法
- tab点击,图片放大缩小动画
- Git_解决库早已经存在问题
- pygame库写游戏——入门<6>——存储图像
- leetCode-Spiral Matrix II
- 关于IP协议首部长度的计算
- 【Scikit-Learn 中文文档】模型选择:选择估计量及其参数
- 【耀阳的读书笔记】算法导论(2)_Θ、O、Ω傻傻分不清楚
- 云南之旅:随想+攻略
- 快速搭建搜索引擎。haystack + whoosh(Django)
- Python学习系列(一)配置环境
- 最小生成树--Prim算法
- 图形用户界面--窗口菜单