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. ...
随机推荐
- Python基础之字符串
字符串内置处理函数 1.capitalize() 描述: 将字符串的第一个字母变成大写,其他字母变小写. 示例: a= "hello world" print (a.capital ...
- bind() unbind()绑定解绑事件
.bind( eventType [, eventData], handler(eventObject)) 本文实例分析了JQuery中Bind()事件用法.分享给大家供大家参考.具体分析如下: .B ...
- C# 根据IP获取省市
/// <summary> /// 根据IP获取省市 /// </summary> public void GetAddressByIp() { string ip = &qu ...
- 高效开发之SASS篇
作为通往前端大神之路的普通的一只学鸟,最近接触了一样稍微高逼格一点的神器,特与大家分享~ 他是谁? 作为前端开发人员,你肯定对css很熟悉,但是你知道css可以自定义吗?大家都知道,js中可以自定义变 ...
- Android_AndroidStudio配置
IDE降低了程序编译的门槛, 让Android程序的编译和运行变得简单易操作. 但无论Eclipse还是Android Studio, IDE都不是非常智能和可靠的, 总会出大大小小的问题. 很多时候 ...
- C/C++-style输入输出函数
C风格的输入输出 (1) int getchar() 与 int putchar(int c) getchar从stdin输入流中读取字符,每次只能读取一个字符.若想一次性读取多个字符,则可将其放入循 ...
- Redis 的几种数据结构&五种数据类型对象
先看几种数据结构 通过分析底层的数据结构,学习如何根据场景选型和设计 1,简单动态字符串 redis使用的字符串SDS有别于C语言中的字符串 a, 结构 free字段为已分配但未使用的空间 len为已 ...
- Gentoo解决Windows系统txt文本中文乱码问题
Linux与Windows系统语言编码区别 在Linux操作系统下,我们有时打开在windows下的txt文件,发现在windows下能正常显示的txt文件出现了中文乱码. 出现这种情况的原因为两种操 ...
- Spark Streaming的wordcount案例
之前测试的一些spark案例都是采用离线处理,spark streaming的流处理一样可以运行经典的wordcount. 基本环境: spark-2.0.0 scala-2.11.0 IDEA-15 ...
- mui 访问手机自带是否连接网络
//mui检测是否连接网络 function getSysInfo() { // var str = ""; // str += "名称:" + plus. ...