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

  这道题目和之前的螺旋矩阵几乎没有区别,而且更简单。同样按照螺旋矩阵的特性,设置4个边界,rowBegin = 0, rowEnd = n-1, colBegin = 0, colEnd = n-1。
  螺旋矩阵的走法:num = 1; 每次把num++代入矩阵
    1.从rowBegin 这一行开始, 从左往右走 [colBegin ~ colEnd],走完把rowBegin++,这行已经走完不需要了。
    2.从colEnd 这一列开始,从上往下走 [rowBegin ~ rowEnd],走完把colEnd--,这列不需要了。
    3.从rowEnd 这一行开始,从右往左走 [colEnd ~ colBegin],走完把rowEnd--, 这行不需要了。
    4.从colBegin 这一列开始,从下往上走 [rowEnd ~ rowBegin],走完把colBegin++,这列不需要了。
 
相比与螺旋矩阵之1, 这题的矩阵都是n*n,所以不需要在每一个for loop 里设置额外条件,因为螺旋矩阵1 里面给的是 m*n 可能不是正方形。
 
 
 

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 (螺旋矩阵之二)的更多相关文章

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

  2. [LeetCode] Spiral Matrix II 螺旋矩阵之二

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

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

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

  5. [Leetcode] spiral matrix ii 螺旋矩阵

    Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...

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

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

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

  8. Leetcode#59 Spiral Matrix II

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

  9. Leetcode59. Spiral Matrix II螺旋矩阵2

    给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, ...

随机推荐

  1. birt 报表设计总结

    1, 通过sql查询出来的数据,当某个字段没有值时,我们期望显示别的东东 在表格单元格或者网格中选中这个值, 在属性编辑器-->映射--> 在映射表中添加映射条件 映射中当使用 等于 时, ...

  2. Zabbix通过SNMP监控多核CPU使用率时, 计算CPU平均使用率

    环境:没有Agent,只能通过SNMP监控时,需要获取多核CPU的平均使用率. ZABBIX的使用SNMP监控CPU使用率时,由于设备都是多核CPU,监控的都是单独某一核心的使用率,但单独某一核使用率 ...

  3. [Node.js] 2、利用node-git-server快速搭建git服务器

    本文用到了node-git-server 1.检测本地git版本 该包的使用需要机器上本来就安装git,且git的版本大于等于2.7: ╭─root@lt /home/workspace ╰─# gi ...

  4. Spring-boot:5分钟整合Dubbo构建分布式服务

    概述: Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合).从服务模型的角度来看,Dubbo采用的是一种非常 ...

  5. Linux脚本入门(1)

    1. Linux 脚本编写基础1.1 语法基本介绍1.1.1 开头程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在这个例 ...

  6. 再起航,我的学习笔记之JavaScript设计模式28(委托模式)

    ## 委托模式 ### 概念介绍 **委托模式(Entrust): **多个对象接收并处理同一请求,他们将请求委托给另一个对象统一处理请求. ### 利用委托优化循环 如果我们有一个需求需要让用户点击 ...

  7. 1001 数组中和等于K的数对 1002 数塔取数问题 1003 阶乘后面0的数量 1004 n^n的末位数字 1009 数字1的数量

    1001 数组中和等于K的数对 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K ...

  8. Variational Bayes

    一.前言 变分贝叶斯方法最早由Matthew J.Beal在他的博士论文<Variational Algorithms for Approximate Bayesian Inference> ...

  9. ajax url参数中文乱码解决方法

    较好的处理办法,对js的url中的中文参数值使用两次encodeURI(),即encodeURI(encodeURI("url的中文参数值")) JS代码: var name=&q ...

  10. c#(asp.net) 多线程示例,用于同时处理多个任务

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...