leetcode59. Spiral Matrix II
来源:互联网 发布:调查问卷数据分析方法 编辑:程序博客网 时间:2024/06/05 09:37
59.Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3
,
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]
题目大意:给定一个整数n,将1-n^2数分别顺时针从最外层到里层直到最中间格子。
思路:总循环当num<=n^2,及格子未填满
每填一个格子都要判断该格子是否已填。条件mart[x][y]==0
依次 1.填上面一行 2.填右边·一行 3.填下面一行 4.填左边一行
AC代码:
public int[][] generateMatrix(int n) {
int[][] mart=new int [n][n];
int squre=n*n;
int x=0,y=0;
int num=1;
while(num<=squre){
while(y<n&&mart[x][y]==0){
mart[x][y]=num;
y++;
num++;
}
y--;
x++;
while(x<n&&mart[x][y]==0){
mart[x][y]=num;
x++;
num++;
}
x--;
y--;
while(y>=0&&mart[x][y]==0){
mart[x][y]=num;
y--;
num++;
}
y++;
x--;
while(x>=0&&mart[x][y]==0){
mart[x][y]=num;
x--;
num++;
}
x++;
y++;
}
return mart;
}
- Leetcode59 Spiral Matrix II
- Leetcode59. Spiral Matrix II
- leetcode59. Spiral Matrix II
- leetcode59. Spiral Matrix II
- LeetCode59——Spiral Matrix II
- leetcode59-Spiral Matrix II(生成蛇形矩阵)
- Spiral Matrix && Spiral Matrix II
- LeetCode: Spiral Matrix II
- LeetCode Spiral Matrix II
- [Leetcode] Spiral Matrix II
- LeetCode : Spiral Matrix II
- Spiral Matrix II
- [LeetCode] Spiral Matrix II
- Spiral Matrix II
- Spiral Matrix II
- [LeetCode]Spiral Matrix II
- Spiral Matrix II
- Spiral Matrix I(II)
- linux/树莓派 ssh连接中断后恢复会话的方法-screen
- HDOJ1786 完全背包+分段处理
- 寻路定点移动
- USNews2017世界大学排行榜
- centos7+nginx+php安装与配置
- leetcode59. Spiral Matrix II
- 查看Oracle数据库中表的历次统计信息收集的sql
- 实战详解使用PHP Manager for IIS搭建win10系统 IIS 10.0+PHP+MySQL环境
- 2017.08.25 阿里笔试题:自由之路
- Mysql一对多 数据数据查询 数据库
- 剑指offer:替换空格
- 线性方程组个数和方程未知数与方程解的关系
- 初识Spring
- Ubuntu16.04+Cuda8.0+Cudnn+Matlab2014a+OpenCV+Caffe 吐血安装教程