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的更多相关文章

  1. 【一天一道LeetCode】#59. Spiral Matrix II

    一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

  2. 【leetcode】59.Spiral Matrix II

    Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...

  3. 【LeetCode】59. Spiral Matrix II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...

  4. 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 ...

  5. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  6. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

  7. [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 ...

  8. Leetcode#59 Spiral Matrix II

    原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...

  9. 59. Spiral Matrix II

    题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...

随机推荐

  1. leetcode medium

    419. Battleships in a Board --No iven an 2D board, count how many different battleships are in it. T ...

  2. Tiny6410之重定位代码到SRAM+4096

    重定位代码 两个不同的地址概念: 对于程序而言,需要理解两个地址,一个是程序当前所处的地址,即程序运行时所处的当前地址.二是程序应该位于的运行地址,即编译程序时所指定的程序的链接地址.在Tiny641 ...

  3. 微信小程序推荐

    工具类 快递100助手:查快递,寄快递. 驾考帮手:为您提供科目一.科目四的测试题练习. 翻译e:多国语言在线翻译,中英.中日.中俄.中法快速翻译,专业翻译. 朝夕日历Pro:一个日历管理 app,你 ...

  4. jsp中使用java函数

    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> ${fn ...

  5. 无线hacking系统—wifislax

    简介 官方中文网站: http://wifislax.cn/ WiFiSlax 是在Slax基础上定制出来的,由西班牙开发.它包含了各种各样的安全和诊断工具.该发行主要的成名原因是把各种各样的非官方网 ...

  6. DP! | 不要怂!

    跟一个博客刷: http://blog.csdn.net/cc_again/article/details/25866971 一.简单基础dp 1.递推 HDU 2084 #include <b ...

  7. 引用AForge.video.ffmpeg,打开时会报错:找不到指定的模块,需要把发行包第三方文件externals\ffmpeg\bin里的dll文件拷到windows的system32文件夹下。

    引用AForge.video.ffmpeg,打开时会报错:找不到指定的模块,需要把发行包第三方文件externals\ffmpeg\bin里的dll文件拷到windows的system32文件夹下. ...

  8. Python学习笔记——基础篇【第七周】———类的静态方法 类方法及属性

    新式类和经典类的区别 python2.7 新式类——广度优先 经典类——深度优先 python3.0 新式类——广度优先 经典类——广度优先 广度优先才是正常的思维,所以python 3.0中已经修复 ...

  9. iOS错误之-Presenting view controllers on detached view controllers is discouraged

    遇到这个警告后找了一下网络资源,基本上只说通过 [self.view.window.rootViewController presentViewController:controller animat ...

  10. 负载均衡lvs_dr_tcp_http单调度

    准备三台虚拟,均为CentOS6.5 x86_64注意,配置过程中,保持端口的一致性.director (eth0 192.168.1.189, vip eth0:0: 192.168.1.18) D ...