LeetCode 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,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
题目标签:Array
Java Solution:
Runtime beats 57.87%
完成日期:07/20/2017
关键词:Array
关键点:螺旋矩阵的遍历模式;设置四个边界值
public class Solution
{
public int[][] generateMatrix(int n)
{
int[][] res = new int[n][n]; int total = n*n;
int num = 1; int rowBegin = 0;
int rowEnd = n-1;
int colBegin = 0;
int colEnd = n-1; while(num <= total)
{
// traverse right (y changes)
for(int y=colBegin; y<=colEnd; y++)
res[rowBegin][y] = num++; rowBegin++; // move down one row // traverse down (x changes)
for(int x=rowBegin; x<=rowEnd; x++)
res[x][colEnd] = num++; colEnd--; // move left one column // traverse left (y changes)
for(int y=colEnd; y>=colBegin; y--)
res[rowEnd][y] = num++; rowEnd--; // move up one row // traverse up (x changes)
for(int x=rowEnd; x>=rowBegin; x--)
res[x][colBegin] = num++; colBegin++; // move right one column } return res;
}
}
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 59. 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 螺旋矩阵之二
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] 885. Spiral Matrix III 螺旋矩阵之三
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...
- [Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...
- LeetCode 54. Spiral Matrix(螺旋矩阵)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
- Leetcode#59 Spiral Matrix II
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
- Leetcode59. Spiral Matrix II螺旋矩阵2
给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, ...
随机推荐
- ionic 打包安卓包
一.配置环境: 先按照之前的文章,配置好环境需要: 二.安装 1. 这里前提是 需要安装 node (地址: http://nodejs.cn/download/) 命令: node -v // ...
- Intellij idea 断点调试
前言 之前使用Intellij Idea断点调试都是极其简单的,都是下一步下一步下一步这样子-..还有最坑爹的以为:IDEA只能调试一次.调试完就要重启Tomcat服务器-..因此花了大量的冤枉时间- ...
- cnblogs第一天
2017-08-25 21:27:16 今天算是真的下定决心要好好的去经营自己的博客了. flag也就不立了,毕竟,flag这种东西立了就是为了打脸的嘛....... 既然说是经营了,那必然是要认认真 ...
- jQuery基礎知識
jQuery基礎知識 $(function(){}) //jQuery先執行一遍再執行其他函數 $(document).ready(fn) //文檔加載完後觸發 1. 刪除$:jQuery.noCon ...
- oracle存储过程中is和as区别
在存储过程(PROCEDURE)和函数(FUNCTION)中没有区别:在视图(VIEW)中只能用AS不能用IS:在游标(CURSOR)中只能用IS不能用AS.
- Day2 基本数据类型
一.python数据类型 1.1数字 2 是一个整数的例子. 长整数 不过是大一些的整数. 3.23和52.3E-4是浮点数的例子.E标记表示10的幂.在这里,52.3E-4表示52.3 * 10-4 ...
- Spring3.2不支持jdk8
解决方案: http://stackoverflow.com/questions/24128045/spring-context-initialization-failed-with-java-lan ...
- Vuforia开发完全指南(四)--- Image Target
Vuforia开发完全指南---Image Target,简单方便的AR图像识别 概述 在Vuforia提供的SDK中,最简单.也是最常见的AR功能就是Image Target---图像识别.你只需提 ...
- spring框架总结(01)
1.spring是什么? sprint其实就是一个开源框架,是于2003年兴起的一个轻量级的java开发框架,是有Road Johnson创建的,简单的来说spring是一个分层的JavaSE/EE( ...
- 点聚合功能---基于ARCGIS RUNTIME SDK FOR ANDROID
一直不更新博客的原因,如果一定要找一个,那就是忙,或者说懒癌犯了. 基于ArcGIS RunTime SDK for Android的点聚合功能,本来是我之前做过的一个系统里面的一个小部分,今天抽出一 ...