Leetcode54. Spiral Matrix螺旋矩阵
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
示例 1:
输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] 输出: [1,2,3,6,9,8,7,4,5]
示例 2:
输入: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12] ] 输出: [1,2,3,4,8,12,11,10,9,5,6,7]
感觉很乱,有空的时候重新改一下
class Solution {
public:
vector<int> spiralOrder(vector<vector<int> >& matrix)
{
vector<int> res;
int r = matrix.size();
if(r == 0)
return res;
int c = matrix[0].size();
int all = r * c;
int up = 0;
int down = r - 1;
int right = c - 1;
int left = 0;
int cnt = 0;
int i = 0, j = 0;
int h = 0;
int v = 0;
for(int t = 0; cnt <= all; t++)
{
// horizontal
if(t % 2 == 0)
{
if(h % 2 == 0)
{
for(; j <= right; j++)
{
res.push_back(matrix[i][j]);
cnt++;
if(cnt == all)
break;
}
j--;
i++;
up++;
h++;
}
else
{
for(; j>= left; j--)
{
res.push_back(matrix[i][j]);
cnt++;
if(cnt == all)
break;
}
j++;
i--;
down--;
h++;
}
}
//vertical
else
{
if(v % 2 == 0)
{
for(; i <= down; i++)
{
res.push_back(matrix[i][j]);
cnt++;
if(cnt == all)
break;
}
i--;
j--;
right--;
v++;
}
else
{
for(; i >= up; i--)
{
res.push_back(matrix[i][j]);
cnt++;
if(cnt == all)
break;
}
i++;
j++;
left++;
v++;
}
}
if(cnt == all)
break;
}
return res;
}
};
其他方法:
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> result;
if (matrix.size() == 0) {
return result;
}
int m = matrix.size(), n = matrix[0].size();
int rowBegin = 0, rowEnd = m - 1, colBegin = 0, colEnd = n - 1;
while (rowBegin <= rowEnd && colBegin <= colEnd) {
for (int i = colBegin; i <= colEnd; ++i ) {
result.push_back(matrix[rowBegin][i]);
}
rowBegin++;
if (rowBegin > rowEnd) {
break;
}
for (int i = rowBegin; i <= rowEnd; ++i) {
result.push_back(matrix[i][colEnd]);
}
colEnd--;
if (colBegin > colEnd) {
break;
}
for (int i = colEnd; i >= colBegin; --i) {
result.push_back(matrix[rowEnd][i]);
}
rowEnd--;
if (rowBegin > rowEnd) {
break;
}
for (int i = rowEnd; i>= rowBegin; --i) {
result.push_back(matrix[i][colBegin]);
}
colBegin++;
}
return result;
}
};
Leetcode54. Spiral Matrix螺旋矩阵的更多相关文章
- 第29题:LeetCode54:Spiral Matrix螺旋矩阵
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...
- Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- [LeetCode] 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 (螺旋矩阵)
此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- [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 ...
- 【LeetCode】Spiral Matrix(螺旋矩阵)
这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...
- spiral matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- [算法][LeetCode]Spiral Matrix——螺旋矩阵
题目要求 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spir ...
随机推荐
- java接口的意义
java当中继承一个接口,要重写他的方法的话,那为什么还要多此一举的去实现一个接口呢? 直接把方法写在类当中不就可以了?就是说去掉类名后面的Implements 接口 ,可以不可以呢? 接口的最主要的 ...
- Spring SpringMVC SpringBoot SpringCloud 注解整理大全
Spring SpringMVC SpringBoot SpringCloud 注解整理 才开的博客所以放了一篇以前整理的文档,如果有需要添加修改的地方欢迎指正,我会修改的φ(๑˃∀˂๑)♪ Spri ...
- nprogress 转
转载:http://www.xuanfengge.com/front-end-nprogress-and-lightweight-web-progress-bar-nanobar.html 前言 进度 ...
- 05-python 学习第五天-简单验证码
通过python 随机数可以制作简单的验证码. 1.0版本来了,这验证码,只有一个码,功能虽然达不到,逻辑还是准确的,目前还不能算是验证码,但是我们会继续完善的. import random # 导入 ...
- Maven编译资源文件拷贝
<build> <finalName>op-balance-job-service</finalName> <plugins> <plugin&g ...
- 深度学习(二十六)Network In Network学习笔记
深度学习(二十六)Network In Network学习笔记 Network In Network学习笔记 原文地址:http://blog.csdn.net/hjimce/article/deta ...
- Leetcode937. Reorder Log Files重新排列日志文件
你有一个日志数组 logs.每条日志都是以空格分隔的字串. 对于每条日志,其第一个字为字母数字标识符.然后,要么: 标识符后面的每个字将仅由小写字母组成,或: 标识符后面的每个字将仅由数字组成. 我们 ...
- 【核心核心】5.Spring【DI】注解方式
使用注解的方式依赖注入不用提供set方法 1.普通类型的注解 @Value @Value(value="春天") private String name; 2.对象类型的注解 @A ...
- Swoole协程报错 Uncaught Error: Call to undefined function go()
解决方法, 在PHP.ini中开启短名
- Ionic 新闻类别菜单
1.效果图 2.controller .js .controller("ProductCtrl", function ($scope,$ionicModal,$ionicScr ...