54. 59. Spiral Matrix
1.
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> ans;
int m = matrix.size();
if(m < )
return ans;
int n = matrix[].size(), size = m*n, x1=, x2=m-, y1=, y2=n-, i, j, k=;
while(k < size)
{
for(i=y1; i<=y2; i++)
{
ans.push_back(matrix[x1][i]);
k++;
}
for(i=x1+; i<=x2; i++)
{
ans.push_back(matrix[i][y2]);
k++;
}
for(i=y2-; x2>x1 && i>=y1; i--)
{
ans.push_back(matrix[x2][i]);
k++;
}
for(i=x2-; y2>y1 && i>x1; i--)
{
ans.push_back(matrix[i][y1]);
k++;
}
x1++; x2--; y1++; y2--;
}
return ans;
}
注意:
下面和左面的边,即第三个和第四个for循环,要加条件x2>x1和y2>y1,否则会有重复元素。
2.
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 ]
]
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> ans(n, vector<int>(n, ));
int size = n*n, x1=, x2=n-, y1=, y2=n-, i, j, k=;
while(k < size)
{
for(i=y1; i<=y2; i++)
ans[x1][i] = ++k;
for(i=x1+; i<=x2; i++)
ans[i][y2] = ++k;
for(i=y2-; x2>x1 && i>=y1; i--)
ans[x2][i] = ++k;
for(i=x2-; y2>y1 && i>x1; i--)
ans[i][y1] = ++k;
x1++; x2--; y1++; y2--;
}
return ans;
}
54. 59. Spiral Matrix的更多相关文章
- leetcode@ [54/59] Spiral Matrix & Spiral Matrix II
https://leetcode.com/problems/spiral-matrix/ Given a matrix of m x n elements (m rows, n columns), r ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- Leetcode 54. Spiral Matrix & 59. Spiral Matrix II
54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
- 59. Spiral Matrix II(中等,同54题)
Given an integer \(n\), generate a square matrix filled with elements from 1 to \(n^2\) in spiral or ...
- 【一天一道LeetCode】#59. Spiral Matrix II
一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- [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 ...
- 【LeetCode】59. Spiral Matrix II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...
- 59. Spiral Matrix && Spiral Matrix II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
随机推荐
- python的时间处理-time模块
time模块 时间的表示方法有三种: 时间戳:表示的是从1970年1月1日0点至今的秒数 格式化字符串表示:这种表示更习惯我们通常的读法,如2018-04-24 00:00:00 格式化元祖表示:是一 ...
- Idea中Maven仓库配置会自动恢复
手头有好几个项目,关闭一个项目,打开另一个项目,发现又在重新下载jar包,打开设置一看,maven配置又恢复到了.m2下边.idea配置的maven会自动恢复吗? 答案是否定的,idea的设置有两个, ...
- 20145122《Java程序设计》第九周学习总结
教材学习内容总结 1.JDBC代表Java数据库连接,这是一个标准的Java API与数据库无关的与Java编程语言之间的和大多数数据库连接.JDBC API支持两层和三层的处理模式对数据库的访问,但 ...
- 20145333茹翔 Exp5 利用nmap扫描
20145333茹翔 Exp5 利用nmap扫描 实验过程 首先使用命令创建一个msf所需的数据库 service postgresql start msfdb start 使用命令msfconsol ...
- 0x30、0x37
1.write_date(0x30+shi)加0x30是什么意思 答: 将数字0-9转化为字符'0'-'9' 1.write_date(0x37+bai)加0x37是什么意思 答: 将大于9的数字转化 ...
- SPOJ Hacking(字典树 + 搜索)题解
思路1:字典树存每个串,然后dfs遍历是否存在.这里有个技巧,如果每次都重新初始化字典树为-1,那么会超时,所以我先初始化为-1,然后设一个Case,每个test时Case都++,那么只要开一个数组判 ...
- Linux 文件的权限
备注 : -rw-r--r-- 第一个“-”不算 ,三个一组 这个就是 644 二.使用chown命令更改文件拥有者 在 shell 中,可以使用chown命令来改变文件所有者.chown命令是c ...
- Linux——软件包简单学习笔记
Linux中的是那种软件包: (这里学习是基于redHat的Cent-OS) 1: 二进制软件包管理(RPM.YUM) 2:源代码包安装 3: 脚本安装(Shell或Java脚本) 一: 二进制软件 ...
- shell 函数调用
例一 #!/bin/bash create_link() { filelist=`ls $` for file in $filelist do echo $/$file done } create_l ...
- 使用 apply 函数族
之前,我们讨论过可以使用 for 循环,在一个向量或列表上进行迭代,重复执行某个表达式.但是在实践中,for 循环往往是最后的选择,因为每次迭代都是相互独立的,所以我们可以使用更简洁更方便的读写方式来 ...