[LC] 59. Spiral Matrix II
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
Example:
Input: 3
Output:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
class Solution {
public int[][] generateMatrix(int n) {
int[][] matrix = new int[n][n];
int beginRow = 0, endRow = n - 1;
int beginCol = 0, endCol = n - 1;
int num = 1;
while(beginRow <= endRow && beginCol <= endCol) {
for(int i = beginCol; i <= endCol; i++) {
matrix[beginRow][i] = num;
num += 1;
}
beginRow += 1;
for (int i = beginRow; i <= endRow; i++) {
matrix[i][endCol] = num;
num += 1;
}
endCol -= 1;
// no need to check boarder b/c it is square
for (int i = endCol; i >= beginCol; i--) {
matrix[endRow][i] = num;
num += 1;
}
endRow -= 1;
for (int i = endRow; i >= beginRow; i--) {
matrix[i][beginCol] = num;
num += 1;
}
beginCol += 1;
}
return matrix;
}
}
[LC] 59. Spiral Matrix II的更多相关文章
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- 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 ...
- 【leetcode】59.Spiral Matrix II
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...
- Leetcode#59 Spiral Matrix II
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
- 59. Spiral Matrix II
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- LeetCode OJ 59. Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 59. Spiral Matrix II(中等,同54题)
Given an integer \(n\), generate a square matrix filled with elements from 1 to \(n^2\) in spiral or ...
- 【一天一道LeetCode】#59. Spiral Matrix II
一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
随机推荐
- NBU For Windows 更新后无法启动Java Console
系统环境:Win Server 2016 备份软件:NBU 8.1 Case :Windows系统默认自动安装系统补丁,重启过程中自动进行了Update,打上了最新的补丁后,NBU Java Co ...
- while read line do done < file
zzx@zzx120:~/test1$ cat file.txt 1122zzx@zzx120:~/test1$ cat ./read.sh #!/bin/bashwhile read line ...
- h5-渐变的基本描述
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JSP页面中提示JSTL标签无法找到的错误
无法解析标签库的错误 1.应该是项目中少了jstl.jar和 standard.jar这两个jar包. 下载地址:https://www.onlinedown.net/soft/1162736.htm ...
- Oracle数据库中表的imp&exp
在Oracle数据库中可以使用imp和exp命令来执行数据的导入导出(包括表结构和数据),使用imp和exp命令执行导入导出操作必需的是需要安装Oracle数据库,系统安装Oracle数据库,可以识别 ...
- 2018.11.2JavaScript随笔
构造函数首字母大写 通过new创建对象 BOM:浏览器对象模型
- VMware vSphere虚拟化-VMware ESXi 5.5组件添加本地磁盘--虚拟机扩容
本地存储器可以是位于ESXi主机内部的内部硬盘,也可以是位于主机之外并直接通过SAS或者SATA等协议连接在主机上的外部存储系统.本地存储不需要存储网络即可与主机进行通信,只需要一根连接到存储单元的电 ...
- 分布式场景下Kafka消息顺序性的思考
如果业务中,对于kafka发送消息异步消费的场景,在业务上需要实现在消费时实现顺序消费, 利用kafka在partition内消息有序的特点,消息消费时的有序性. 1.在发送消息时,通过指定parti ...
- ios ktvhttpcache 音视频缓存插件使用
1.PodFile 文件增加 pod 'KTVHTTPCache', '~> 2.0.0' 2.在终端 需要先cd到podfile文件所在目录 执行pod install 3.在header ...
- 编译Python文件
编译Python文件 一.编译Python文件 为了提高加载模块的速度,强调强调强调:提高的是加载速度而绝非运行速度.python解释器会在__pycache__目录中下缓存每个模块编译后的版本,格式 ...