[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 ...
随机推荐
- Nginx反向代理 Laravel获取真实IP地址(PHP)
使用VUE前后端分离开发 后端使用Laravel 想要获取到用户的真实IP地址 因为分离开发不同源跨域问题 所以只能进行前端Nginx反向代理 location /api { rewrite ^/a ...
- html+php上传图片文件到服务器
html+php上传图片文件到服务器 一.html代码 <body> <form action="" method="post" enctyp ...
- SHELL里执行HIVE导出文件处理成CSV文件
#!/bin/bash #用途: #.当前目录的txt文件批量转csv #.制表符转逗号分隔符 #.NULL去除 #.删除WARN警告 for i in `ls ./*.txt` do sed -e ...
- 【Java】关于Spring MVC框架的总结
SpringMVC是一种基于Java,实现了Web MVC设计模式,请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将Web层进行职责解耦.基于请求驱动指的就是使用请求-响应模型,框架的 ...
- LeetCode:9. Palindromic Number(Medium)
原题链接:https://leetcode.com/problems/palindrome-number/description/ 1. 题目要求:判断一个int类型整数是否是回文,空间复杂度O(1) ...
- 人工智能,图片识别,与GUI编程
GUI编程: https://sourceforge.net/projects/pyqt/ 百度aip图片识别 https://pypi.python.org/pypi/baidu-aip
- SharedPreferences Android
类似iOS的NSUserDefaults,采用key-value(键值对)形式,主要用于轻量级的数据存储 public class MainActivity extends AppCompatActi ...
- CCF-NOIP-2018 提高组(复赛) 模拟试题(四)
T1 贪吃蛇 [问题描述] 贪吃蛇是一个好玩的游戏.在本题中,你需要对这个游戏进行模拟. 这个游戏在一个 \(n\) 行 \(m\) 列的二维棋盘上进行. 我们用 \((x, y)\) 来表示第 \( ...
- Spring实战第六章学习笔记————渲染Web视图
Spring实战第六章学习笔记----渲染Web视图 理解视图解析 在之前所编写的控制器方法都没有直接产生浏览器所需的HTML.这些方法只是将一些数据传入到模型中然后再将模型传递给一个用来渲染的视图. ...
- Captcha 验证码Example
maven依赖 防止和spring中的servlet冲突 <dependency> <groupId>com.github.penggle</groupId> &l ...