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 example,
Given n = 3,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
【题目分析】
与Spiral Matrix不同,这个题目要求生成一个方阵,方阵的值是螺旋递增的。
【思路】
其实这个题目与上一个大同小异,他们遍历矩阵的顺序是相同的,只要把取值变为赋值就可以了。
【java代码】
public class Solution {
public int[][] generateMatrix(int n) {
int[][] matrix = new int[n][n];
int top = 0;
int bottom = n-1;
int left = 0;
int right = n-1;
int num = 1;
while(true){
for(int i = left; i <= right; i++) matrix[top][i] = num++;
top++;
if(left > right || top > bottom) break;
for(int i = top; i <= bottom; i++) matrix[i][right] = num++;
right--;
if(left > right || top > bottom) break;
for(int i = right; i >= left; i--) matrix[bottom][i] = num++;
bottom--;
if(left > right || top > bottom) break;
for(int i = bottom; i >= top; i--) matrix[i][left] = num++;
left++;
if(left > right || top > bottom) break;
}
return matrix;
}
}
LeetCode OJ 59. Spiral Matrix II的更多相关文章
- 【一天一道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
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...
- 【LeetCode】59. Spiral Matrix II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...
- 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 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
- [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& ...
- 59. Spiral Matrix II
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
随机推荐
- 【Map】Double Queue
Double Queue Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13258 Accepted: 5974 Des ...
- css1
背景属性:background;background-color;background-image;background-repeat;(背景是否重复,有repeat-x 横式重复,repeat-y竖 ...
- 总结:liunx常见命令集合
没有系统学习过liunx,把工作中遇到的liunx命令集合信息如下: 1.nc传送文件 之前总是用rsync, 今天遇到了一个从阿里云服务器传送文件到我们公司的内网服务器,这就不能传了,又想用一致的文 ...
- [UWP小白日记-6]页面跳转过度动画
前言 在学习中发现页面导航默认是没有过度动画的,直接就导航过去太粗暴了( ̄へ ̄),于是打算上动画结果不言而喻自己进了坑完全不懂动画,然后就是各种疯狂(´・_・`)的搜索资料看了后终于有点头绪. 再后来 ...
- 关于mongodb的一些笔记
1.以服务的形式安装mongodb dos -- 进入到mongodb的bin目录下,执行 D:\mongodb\bin>mongod --logpath D:\mongodb\logs\mon ...
- 366. Find Leaves of Binary Tree C#
Example:Given binary tree 1 / \ 2 3 / \ 4 5 Returns [4, 5, 3], [2], [1]. Explanation: 1. Removing th ...
- NSRange:NSMakeRange
NSRange:NSMakeRange(6, cardNo.length - 10) [cardNo stringByReplacingCharactersInRange:NSMakeRange(6, ...
- python修炼7----迭代器
迭代器 -------------------------------------------------------------------------------- 充电小知识 1.yield-- ...
- 如何设置linux的文件和目录的权限
1 字符表示法 1)chmod [-R] 权限(mode)file 注:[-R]会将目录下所有权限都设置成指定的 who operator permission u(owner) +增加权限 r( ...
- 如何占用你用户的时间 and 如何提高客户的满意度 。 待续
未来的商业竞争, 可能本质上是在争取客户的时间 嗯..有不定时, 未知的奖励,游戏行业就经常使用, 比如打怪掉装备, 不一定掉什么好东西, 让人充满了期待, 玛雅宝石, 有一定的概率... 觉得公司员 ...