LeetCode 58 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
,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
思路:与上一篇类似。仅仅要不越界就ok
public class Solution {
public int[][] generateMatrix(int n) {
if (n == 0)
return new int[0][0]; int[][] matrix = new int[n][n];
int x1 = 0;
int y1 = 0;
int x2 = matrix.length - 1;
int y2 = matrix[0].length - 1;
int i = 0, j = 1;
while (x1 <= x2 && y1 <= y2) {
// up row
for (i = y1; i <= y2; ++i, j++)
matrix[x1][i] = j;
// right column
for (i = x1 + 1; i <= x2; ++i, j++)
matrix[i][y2] = j;
// bottom row
for (i = y2 - 1; x2 != x1 && i >= y1; --i, j++)
matrix[x2][i] = j;
// left column
for (i = x2 - 1; y1 != y2 && i > x1; --i, j++)
matrix[i][y1] = j; x1++;
y1++;
x2--;
y2--;
}
return matrix;
}
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
LeetCode 58 Spiral Matrix II的更多相关文章
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- Java for LeetCode 059 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(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
- 【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
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
- 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螺旋遍历矩阵2
Given a positive integer n, generate a square matrix filled with elements from 1 to n^2 in spiral or ...
- LeetCode题目:Spiral Matrix II
原题地址:https://leetcode.com/problems/spiral-matrix-ii/ class Solution { public: vector<vector<in ...
随机推荐
- nginx 配置访问正则匹配
server{ listen 80; server_name api.zyy.com; root /var/www/api_zyy; index index.php; location ~ /asse ...
- display属性解析
none 此元素不会被显示 block 此元素将显示为块级元素,此元素前后会带有换行符. inline 默认.此元素会被显示为内联元素,元素前后没有换行符. inline-block 行内块元素.(C ...
- 前端 HTML基础
前端三大利器概述 学习前端,不得不学习前端中的三大利器:html + css + javascript.那么这三个组件分别起到什么作用呢?以人体为例,单单具有html属性的人,只是一个裸体的人偶(理解 ...
- Aspose.Words导出dt到word的问题
已解决:单挑数据导入到一个word文档,导出文字和图片成功,执行Main();方法导出dt到word 待解决:多条数据(文字,图片的导入到一个word文档里面)从dt导入到word;已发现的错误提示“ ...
- Node.js中的exports与module.exports的区分
1. module应该是require方法中,上下文中的对象 2. exports对象应该是上下文中引用module.exports的新对象 3. exports.a = xxx 会将修改更新到mod ...
- JavaWeb三大组件(Servlet,Filter,Listener 自己整理,初学者可以借鉴一下)
JavaWeb三大组件(Servlet,Filter,Listener 自己整理,初学者可以借鉴一下) Reference
- (转)ASP.NET MVC 学习第一天
天道酬勤0322 博客园 | 首页 | 发新随笔 | 发新文章 | 联系 | 订阅 | 管理 随笔:10 文章:0 评论:9 引用:0 ASP.NET MVC 学习第一天 今天开始第一天学习as ...
- JS 匿名函数
一.声明: 1. 正常函数声明: //正常函数声明 function foo(p1, p2){ return p1+p2; } 2. 匿名函数声明: //匿名函数声明 var foo= functio ...
- JS中,如何查询一个对象的所有属性
var res = ""; for(var p in object) { res += p + ","; } alert(res);
- Microsoft Visual C++ 2005 SP1 Redistributable 安装错误
1.在安装Microsoft Visual C++ 2005 SP1 Redistributable时报错:Command line option syntax error.Type Command ...