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 ...
随机推荐
- (一)github之基础概念篇
1.github: 一项为开发者提供git仓库的托管服务, 开发者间共享代码的场所.github上公开的软件源代码全都由git进行管理. 2.git: 开发者将源代码存入名为git仓库的资料库中,而g ...
- SNMP学习笔记之SNMP简单概述
0x00 SNMP简单概述 0.1.什么是Snmp SNMP是英文"Simple Network Management Protocol"的缩写,中文意思是"简单网络管理 ...
- Python3 爬取微信好友基本信息,并进行数据清洗
Python3 爬取微信好友基本信息,并进行数据清洗 1,登录获取好友基础信息: 好友的获取方法为get_friends,将会返回完整的好友列表. 其中每个好友为一个字典 列表的第一项为本人的账号信息 ...
- MAC下BurpSuit社区版升级pro版
环境OSX 步骤1. 官网下载社区版v1.7.32https://portswigger.net/burp/communitydownload 安装 2. 下载keygenhttps://down.5 ...
- 【附9】elasticsearch-curator + Linux定时任务
官网教程入口:https://www.elastic.co/guide/en/elasticsearch/client/curator/current/index.html 一.下载安装 下载:sud ...
- about MySQL Workbench的基本使用及运用操作
http://blog.csdn.net/dongdong9223/article/details/48318877 <大神整理的更好!(评论里还有其他整理的) ↑ 使用MySQL Work ...
- 【jdk源码分析】ArrayList的size()==0和isEmpty()
先看结果 分析源码 [jdk源码解析]jdk8的ArrayList初始化长度为0 java的基本数据类型默认值 无参构造 size()方法 isEmpty()方法
- 【TCP/IP详解 卷一:协议】第十一章 UDP 用户数据报协议
11.1 引言 UDP 是一个简单的 面向数据报 的运输层协议:进程的每个 输出操作 都正好产生一个 UDP数据报,并且组装成一份待发送的IP数据报. 这与 TCP 不一样,它是 面向流字符 的协议, ...
- UOJ 171 【WC2016】挑战NPC
一开始还真没想到是一般图匹配这种模型(毕竟才会的带花树) 把每一个盒子拆成3个,每一个可以放置进它的小球分别向这三个点连边,然后这三个点在连成一个三元环,最终答案就是小球数目-匹配数. 由于是一般图, ...
- WPF基础学习笔记整理 (四) 布局
WPF使用的是容器(container)进行布局: WPF窗口(Window类型)只能包含单个元素,故为了放置多个元素并增强界面效果,引入了容器: WPF布局容器都派生自System.Windows. ...