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 ]
]
解题思路:
参考Java for LeetCode 054 Spiral Matrix,修改下代码即可,JAVA实现如下:
static public int[][] generateMatrix(int n) {
if(n==0)
return new int[0][0];
int[][] matrix=new int[n][n];
int num=0,left = 0, right = matrix[0].length - 1, up = 0, down = matrix.length - 1;
while (left <= right && up <= down) {
for (int i = left; i <= right&&up<=down; i++)
matrix[up][i]=++num;
up++;
for (int i = up; i <= down&&left<=right; i++)
matrix[i][right]=++num;
right--;
for (int i = right; i >= left&&up<=down; i--)
matrix[down][i]=++num;
down--;
for (int i = down; i >= up&&left<=right; i--)
matrix[i][left]=++num;
left++;
}
return matrix;
}
Java for LeetCode 059 Spiral Matrix II的更多相关文章
-
[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】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
-
LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
-
059. Spiral Matrix II
题目链接:https://leetcode.com/problems/spiral-matrix-ii/description/ Given a positive integer n, generat ...
-
【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 ...
-
Java for LeetCode 054 Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
-
LeetCode 58 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
随机推荐
-
Matlab 符号运算
root(p):多项式求根.多项式等于0时对应方程的根. 例:,则输入p=[5 4 3 2 1]; root(p) 注:多项式系数都是按幂指数递减形式的. poly([a,b,c]):求已知根为a,b ...
-
【转】NSHashtable and NSMaptable
本文转自Nidom的博客,原文:<NSHashtable & NSMaptable> NSSet, NSDictionary, NSArray是Foundation框架关于集合 ...
-
图片延迟加载技术-Lazyload的应用
我们在浏览图片量非常大的页面时,像淘宝商城商品展示.必应图片搜索这类网站,图片的加载非常流畅,其中就应用了图片延迟加载技术.本文讲解Lazyload图片加载插件,当我们打开页面时,首先在屏幕可视区域加 ...
-
JavaScript主流框架3月趋势总结
原文: What’s New in JavaScript Frameworks-March 2018 译者: Fundebug 为了保证可读性,本文采用意译而非直译.另外,本文版权归原作者所有,翻译仅 ...
-
001.Amoeba读写分离部署
一 Amoeba简介 Amoeba(变形虫)项目,该开源框架于2008年 开始发布一款 Amoeba forMysql软件.这个软件致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQ ...
-
XXE攻击学习
环境:lAMP simplexml_load_string.php代码内容 <?php $data = file_get_contents('php://input'); $xml = simp ...
-
openGL-------------别人的博客
https://blog.csdn.net/dcrmg/article/category/6505957 OpenGL(一)绘制圆.五角星.正弦曲线 ========================= ...
-
学JS的心路历程 -物件与原型(二)
昨天有提到说Object.setPrototypeOf可以指定一个物件为另一个物件的原型,但有想过到底这个原型,也就是[[Prototype]]最终会到何处吗? 答案是Object.prototype ...
-
Liunx touch
http://blog.csdn.net/tanga842428/article/category/6355419 linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时 ...
-
msfpayload反弹shell
1.前期-- 情景就是当我们获得webshell时,我们想留下我们的后门,这个时候我们可以用到msfpayload与msfconsole结合使用 启动PostgreSQL服务:service post ...