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 ...
随机推荐
- ESlint 语法检测配置说明
部分vue-cli脚手架创建的默认eslint规则: 代码末尾不能加分号 ; 代码中不能存在多行空行 tab键不能使用,必须换成两个空格 代码中不能存在声明了但未使用的变量 关闭eslint 这里只说 ...
- 【UVA11806 Cheerleaders】 题解
题目链接:https://www.luogu.org/problemnew/show/UVA11806 容斥原理+组合数 正着找合♂fa的不好找,那就用总方案数-不合♂fa的 #include < ...
- TCP-IP and Advanced Topics 课程总结与报告
课程总结 学习了四周十六课的课程,对每一课的知识点进行总结梳理,作出一个树状的知识网络图. 本课程虽然在深度上有所欠缺,但却更有利于结构上的梳理,加深总体上对网络的理解. 本课程从Internet出发 ...
- 【2017年最新】 iOS面试题及答案
设计模式是什么? 你知道哪些设计模式,并简要叙述? 设计模式是一种编码经验,就是用比较成熟的逻辑去处理某一种类型的事情. 1). MVC模式:Model View Control,把模型 视图 控制器 ...
- android软件开发之获取本地音乐属性
歌曲的名称 :MediaStore.Audio.Media.TITLString tilte = cursor.getString(cursor.getColumnIndexOrThrow(Media ...
- Vue--- vue-cli 脚手架 安装 -reeber
vue-cli 脚手架 安装 一. node安装 1)如果不确定自己是否安装了node,可以在命令行工具内执行: node -v (检查一下 版本): 2)如果 执行结果显示: xx 不是内部命 ...
- tomcat启动startup.bat一闪而过【亲测有效】
遇到很多次运行startup.bat后,一个窗口一闪而过的问题,但是从来没去纠正怎样修改配置才是正确的,现在从网上查阅的资料整理如下:tomcat在启动时,会读取环境变量的信息,需要一个CATALIN ...
- BZOJ2286: [Sdoi2011]消耗战(虚树/树形DP)
Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 5246 Solved: 1978[Submit][Status][Discuss] Descript ...
- chromium之scoped_ptr
看看怎么使用 // Scopers help you manage ownership of a pointer, helping you easily manage the // a pointer ...
- GoogleHacking相关技巧
转自https://www.cnblogs.com/anka9080/p/googlehack.html 0x 01 GoogleHack语法 Site 指定域名 Intext 正文中出现关键字的网页 ...