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

For example,
Given n =3,

You should return the following matrix:

[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]

题意:给定整数n,以螺旋的方式形成一个n×n个矩阵。

思路:这题的思路和spiral matrix 一样。还是按照螺旋的方式去赋值,由外到内的一层层赋值。这两题的区别在于,本题中,不可能剩下1×k或者k×1的区域没有赋值,因为这个是一个正方形,所以在讨论最初和最后情况时,只需要一个if条件判断即可。还有一点要注意的是,返回值一定要先赋值,不然用下标访问不存在的地方会报错。代码如下:

 class Solution {
public:
vector<vector<int> > generateMatrix(int n)
{
vector<vector<int>> matrix(n,vector<int>(n,));
if(n==) return matrix; int left=,right=n-;
int up=,down=n-;
int num=; while(right>=left && down>=up)
{
if(right==left)
{
matrix[up][right]=num;
return matrix;
} for(int i=left;i<right;++i)
{
matrix[up][i]=num;
num++;
}
for(int i=up;i<down;++i)
{
matrix[i][right]=num;
num++;
}
for(int i=right;i>left;i--)
{
matrix[down][i]=num;
num++;
}
for(int i=down;i>up;i--)
{
matrix[i][left]=num;
num++;
} left++;
right--;
up++;
down--;
}
return matrix; }
};

方法二:也可以使用旋转图片中的思想,只不过比较难想一些。代码如下:

 class Solution
{
public:
vector<vector<int>> generateMatrix(int n)
{
vector<vector<int>> matrix(n,vector<int>(n,)); //一定要给matrix给赋值。
// 由外层到里,一层考虑
if(n==)
{
matrix[][]=;
return matrix;
}
int temp=;
//层数
for(int layer=;layer<n/;++layer)
{
int first=layer;
int last=n--layer;
int num=last-first;
//每一层上,每一行、列,找出相应规律,都从顶点赋值
for(int i=first;i<last;++i)
{
int offset=i-first;
matrix[first][i]=temp+i;
matrix[i][last]=temp+num+i;
matrix[last][last-offset]=temp+*num+i;
matrix[last-offset][first]=temp+*num+i;
}
temp+=*num-;
}
//当n为奇数时,最中心的一个为n^2
if(n%==) matrix[n/][n/]=n*n;
return matrix;
}
};

[Leetcode] spiral matrix ii 螺旋矩阵的更多相关文章

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

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

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

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

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

  4. leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?

    Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

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

  6. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

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

  8. PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]

    1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...

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

随机推荐

  1. 介绍几个PHP 自带的加密解密函数

    PHP 自带的加密解密函数 目前经常使用的加密函数有:md5(), sha1(), crypt(), base64_encode(), urlencode() . 其中 md5(), sha1(), ...

  2. Java : java基础(4) 线程

    java开启多线程的方式,第一种是新建一个Thread的子类,然后重写它的run()方法就可以,调用类的对象的start()方法,jvm就会新开一个线程执行run()方法. 第二种是类实现Runabl ...

  3. 软件的按契约设计(DbC---Design by Contract)

    一.DbC基本概念 DbC的思想源于商业活动中商家和用户的行为(义务和利益关系),双方都要遵守一个契约(合同),交易才能完成. 商家与用户的契约关系如下: 1. 商家必须提供某种产品(义务),并有权获 ...

  4. STM32CubeMx配置正交编码器遇到的问题

    配置时参考了这个哥们的方法: http://www.eemaker.com/stm32cubemx-encoder.html 然后我的配置是这样的 配置是没有问题. 调用时出现了问题. 由于配置完了, ...

  5. u-boot-2016.01移植(一)

    1.了解uboot: 阅读uboot源码顶层目录下的README.TXT可以提取如下信息:     made to support booting of Linux images.   //引导内核程 ...

  6. u-boot.bin生成过程分析

    ELF格式“u-boot”文件的生成规则如下,下面对应Makefile的执行过程分别分析各个依赖. $(obj)u-boot: depend version $(SUBDIRS) $(OBJS) $( ...

  7. ABAP CDS ON HANA-(11)ABAP CDSでの関連付け

    Association in ABAP CDS An association in CDS view joins different data sources. Defining and using ...

  8. SQL Server中2008及以上 ldf 文件过大的解决方法

    在SQL Server中经常遇到事务日志变大的情况,除了将数据库设置为“自动收缩”外,还可以使用下面的SQL命令进行快速清除数据库中的事务日志,命令如下:  - 第一步:清空日志  ALTER DAT ...

  9. How to add a webpart to your website

          I have download a webpart that can play media on the website from the internet.Then how to add ...

  10. 【数据结构】 Queue 的简单实现

    [数据结构] Queue 的简单实现 public class XQueue<T> { /// <summary> /// 第一个元素 /// </summary> ...