[Leetcode] spiral matrix ii 螺旋矩阵
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 螺旋矩阵的更多相关文章
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [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 ...
- Leetcode59. Spiral Matrix II螺旋矩阵2
给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, ...
- leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?
Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- [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 ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- 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 ...
- PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]
1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...
- [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 ...
随机推荐
- MySQL索引介绍
引言 今天Qi号与大家分享什么是索引.其实索引:索引就相当于书的目录 索引介绍 用官方的话说就是 索引是为了加速对表中数据行的检索而创建的一种分散的存储结构.索引是针对表而建立的,它是由数据页面以外的 ...
- YII2.0学习二 安装adminlte 后台模板
控制台切换到安装目录wwwroot/shanghai/ 修改一下composer镜像地址:composer 使用中国镜像 运行 composer require dmstr/yii2-adminlte ...
- 【struts2】struts2的使用
1.使用步骤 1) 导入struts2的支持jar包 名称 说明 struts2-core-2.3.4.1.jar Structs2的核心类库 xwork-core-2.3.4.1.jar xwork ...
- ScriptManager和UpdatePanel用法 (ajax)
ScriptManager和UpdatePanel控件联合使用可以实现页面异步局部更新的效果.其中的UpdatePanel就是设置页面中异 步局部更新区域,它必须依赖于ScriptManager存在, ...
- mtools使用-1
mtools是什么? mtools 是一组非常好用的 MongoDB 日志分析工具 ,由MongoDB Inc 官方工程师所写. 组成部分 mlogfilter :按时间切片日志文件,合并日志文件,过 ...
- SQL 公用表表达式(CTE)
1.概念 公用表表达式(Common Table Expression)是SQL SERVER 2005版本之后引入的一个特性.CTE可以看作是一个临时的结果集,可以在接下来的一个SELECT,INS ...
- 设置Git 记住密码
设置记住密码(默认15分钟): git config --global credential.helper cache 如果想自己设置时间,可以这样做: git config credential.h ...
- 1176: [Balkan2007]Mokia
1176: [Balkan2007]Mokia 链接 分析 三维偏序问题,CDQ分治论文题. 代码 #include<bits/stdc++.h> using namespace std; ...
- LeetCode:24. Swap Nodes in Pairs(Medium)
1. 原题链接 https://leetcode.com/problems/swap-nodes-in-pairs/description/ 2. 题目要求 给定一个链表,交换相邻的两个结点.已经交换 ...
- 判断电脑CPU硬件支不支持64位
你可以在注册表中查看: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment\PROCESSO ...