[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 ...
随机推荐
- maven-坐标与依赖
1.坐标-找到项目依赖的重要依据 <groupId>cmbc.com.cn</groupId> <artifactId>myapp</artifactId&g ...
- mybatis报错:查询一对多或多对多时只返回一条数据的问题
问题: 使用映射文件实现查询一对多或多对多时只返回一条数据问题 解决方法: 导致这种情况出现的问题是因为两个表中的主键是一样所以出现了数据覆盖问题. 解决方式一:修改数据库表中的主键(这种方法比较麻烦 ...
- 判断不同浏览器,加载不同的css和js文件
在低版本的IE中,条件注释还有效果,但是在ie9,10,11浏览器中,条件注释不起作用. 在网上找了个校验ie的方法. function isIE(){ if (window.ActiveXObje ...
- ctf题目writeup(4)
2019.1.31 题目:这次都是web的了...(自己只略接触隐写杂项web这些简单的东西...) 题目地址:https://www.ichunqiu.com/battalion 1. 打开链接: ...
- 分布式系统的CAP(Redis)
CAP理论就是说在分布式存储系统中,最多只能实现上面的两点.而由于当前的网络硬件肯定会出现延迟丢包等问题,所以 分区容忍性是我们必须需要实现的. 所以我们只能在一致性和可用性之间进行权衡,没有NoSQ ...
- Verilog学习笔记基本语法篇(七)········ 生成块
生成块可以动态的生成Verilog代码.可以用于对矢量中的多个位进行重复操作.多个模块的实例引用的重复操作.根据参数确定程序中是否包含某段代码.生成语句可以控制变量的声明.任务和函数的调用.还能对实例 ...
- 【转】手把手教你:Ubuntu14+apache2+django1.7+python2.7下网页/网站部署
本人亲自尝试了网上众多的部署网页/网站方法,绝大多数都未能试验成功,这次的项目光部署这块遇到了很多问题,大概耗费了我一个星期. 本着:王道论坛中的赠人玫瑰,手留余香的精神.我把自己一路所走的历程发布出 ...
- C++11中rvalue references的使用
Rvalue references are a feature of C++ that was added with the C++11 standard. The syntax of an rval ...
- 程序在Linux下前后台切换
程序在Linux下前后台切换 一.为什么要使程序在后台执行 背景:SecureCRT远程连接到linux主机,使程序在后台运行有以下好处: (1)本机关机不影响linux主机运行 (2)不影响计算效率 ...
- java容器操作一
List l = new ArrayList(); l.add(1); l.add("ne"); // 获取 System.out.println(l.get(0)); // 判断 ...