LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接
https://leetcode.com/problems/spiral-matrix-ii/description/
2. 题目要求
给定一个正整数n,求出从1到n平方的螺旋矩阵。例如n为3,构成的螺旋矩阵如下图所示

3. 解题思路
该题与54题Spiral Matrix的解题思路大致相同,同样是一个while循环内,确定螺旋矩阵一个圈上的所有元素。
采用一个计数器count,count初始为1,每确定一个位置上的元素,count加1,
4. 代码实现
public class SpiralMatrixII59 {
public static void main(String[] args) {
for(int []n:generateMatrix(3)){
for(int x:n)
System.out.print(x+" ");
System.out.println();
}
}
/**
* t t t t t
* l ... r
* l r
* l ... r
* b b b b r
*/
public static int[][] generateMatrix(int n) {
int[][] res = new int[n][n];
int left = 0, right = n - 1;
int top = 0, bottom = n - 1;
int count = 1;
while (left <=right) {
// 确定t所在边上的元素
for (int i = left; i <= right; i++) {
res[top][i] = count++;
}
top++;
// 确定r所在边上的元素
for (int i = top; i <= bottom; i++) {
res[i][right] = count++;
}
right--;
// 确定b所在边上的元素
for(int i= right;i>=left;i--){
res[bottom][i]=count++;
}
bottom--;
// 确定l所在边上的元素
for(int i =bottom;i>=top;i--){
res[i][left]=count++;
}
left++;
}
return res;
}
}
LeetCode: 59. Spiral Matrix II(Medium)的更多相关文章
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- LeetCode 59. Spiral Matrix II (螺旋矩阵之二)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II
Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...
- Leetcode#59 Spiral Matrix II
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
- LeetCode Spiral Matrix II (技巧)
题意: 从1开始产生连续的n2个数字,以螺旋的方式填满一个n*n的数组. 思路: 由于是填满一个矩阵,那么只需要每次都填一圈即可.应该注意特殊情况. 迭代: class Solution { publ ...
- [leetcode]59. Spiral Matrix II螺旋遍历矩阵2
Given a positive integer n, generate a square matrix filled with elements from 1 to n^2 in spiral or ...
- LeetCode: 63. Unique Paths II(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/
- 【leetcode】Single Number II (medium) ★ 自己没做出来....
Given an array of integers, every element appears three times except for one. Find that single one. ...
- Leetcode 54. Spiral Matrix & 59. Spiral Matrix II
54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...
随机推荐
- 开源项目之防火墙 tdifw
tdifw是windows防火墙软件(TDI层驱动过滤),负责监控网络监听与连接.以及过滤信息. 源码在src目录, 程序在Bin目录,执行根目录下的批处理文件也可以,具体步骤如下: 1. 运行ins ...
- sourcetree创建分支与分支合并
一.Sourcetree简单介绍 通过Git可以进行对项目的版本管理,但是如果直接使用Git的软件会比较麻烦,因为是通过一条一条命令进行操作的. Sourcetree则可以与Git结合,提供图形界面 ...
- Linux关于压缩和解压缩实例
在谈到压缩和解压缩,我想说说它们的应用场景,其实它们主要的应用场景是有这么几个方面? (1)备份(几十个数据库每天进行备份,即包含数据又包含脚本,还有其他十分重要的日志文件等等); (2)降低服务器存 ...
- [转]MFC子线程更改图像数据后更新主窗口图像显示方法
程序思路是由外部的输入输出控制卡发出采集图像信号,之后相机采集图像得到图像数据指针,接收图像数据指针创建成图像最后显示到MFC对话框应用程序的Picture Control控件上,同时,为了标定相机位 ...
- mysql5.7.22tar包安装
mysql5.7.22tar包安装 #卸载系统自带的Mariadb [root@ ~]# rpm -qa|grep mariadb mariadb-libs-5.5.44-2.el7.centos.x ...
- hdu 1021 Fibonacci Again(变形的斐波那契)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1021 Fibonacci Again Time Limit: 2000/1000 MS (Java/Ot ...
- mobBUS
1.今天听陈刚说起modBUS通信协议,这个还是第一次听说,究竟是什么东东,还是上网查查看吧 2.网上有C语言程序. http://blog.163.com/li_g888@126/blog/stat ...
- 使用java原生API模拟请求下载文件
/** * * @param urlPath * 下载路径 * @param saveDir * 下载存放目录 * @return 返回下载文件 * @throws Exception */ publ ...
- oracle之DQL
一.单表查询 语法:select * from table where 条件 group by 分组 having 过滤分组 order by 排序 --查询平均工资低于2000的部门的最大工资和平均 ...
- mysql数据库迁移到oracle数据库后 如何删除相同的数据
mysql数据库迁移到oracle数据库后 如何删除相同的数据 首先搞清楚有多少数据是重复的 select pid from product group by pid having count(pid ...