LeetCode OJ-- Spiral Matrix
https://oj.leetcode.com/problems/spiral-matrix/
螺旋矩阵,逆着转,输出矩阵中的元素。
在纸上模仿,然后记左上角(l1,l2)右上角(l1,r2),左下角(p1,l2)右下角(p1,r2).
然后4个for循环从一个点到另一个点位置遍历。
while控制总的。
当在一次遍历中,没有要输出的点,说明遍历结束。
class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
vector<int> ans;
int row = matrix.size();
if(row == )
return ans;
if(row ==)
{
for(int i = ;i<matrix[].size();i++)
ans.push_back(matrix[][i]);
return ans;
}
int col = matrix[].size(); int l1,l2,r2,p1;
l1 = ;
l2 = ;
r2 = col - ;
p1 = row -; while()
{
int i;
if(l2>r2)
break;
for(i = l2; i <= r2; i++)
ans.push_back(matrix[l1][i]); if(l1+>p1)
break;
for(i = l1+;i<= p1;i++)
ans.push_back(matrix[i][r2]); if(r2-<l2)
break;
for(i = r2-;i>=l2;i--)
ans.push_back(matrix[p1][i]); if(p1-<l1+)
break;
for(i = p1-;i>=l1+;i--)
ans.push_back(matrix[i][l2]);
l1++;
l2++;
r2--;
p1--;
}
return ans;
}
};
LeetCode OJ-- Spiral Matrix的更多相关文章
- Java for LeetCode 059 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 ...
- [LeetCode 题解] Spiral Matrix
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目链接 54. Spiral Matrix ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
- 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] 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 885. Spiral Matrix III
原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
随机推荐
- DC课程目标
- Python学习笔记:面向对象(类)
1.类定义:Python3中,如果新建的类没有继承任何其他类,默认继承基础类object.Python2中如果没有显式继承object类就是经典类,而显式继承了object类就是新式类,Python2 ...
- $(MAKE) , make命令
make 定义了很多默认变量,像常用的命令或者是命令选项之类的,什么CC啊,CFLAGS啊之类.$(MAKE)就是预设的 make 这个命令的名称(或者路径).make -p 可以查看所有预定义的变量 ...
- stm32L0系列学习(二)HAL-LL库等比较
- ACM Changchun 2015 L . House Building
Have you ever played the video game Minecraft? This game has been one of the world's most popular ga ...
- 利用http录制jmeter脚本
1.在WorkBench下新建HTTP(S) Test Script Recorder,默认端口号为8080,假如8080被占用,则使用其他端口号:且为了使录制保存到线程组里,也同时新建一个线程组Tr ...
- P3391 【模板】文艺平衡树(Splay)新板子
P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...
- 评估后Vista时代系统内核模式安全性
Windows Vista与之前的MS Windows版本(包括WindowsXPSP2)相比增加了很多的安全性.Vista新安全性的特征可以包括以下几个方面: 驱动签名 路径保护 内核模式代码完整性 ...
- custom post types 404 Page Error
问题: 注册新的文章类型后,用新的类型写文章,打开后报 404 错误 原因: 因为虽然注册了新的帖子类型,但WordPress还不知道如何处理它 解决: 到设置 -> 固定链接,重新点击保存,再 ...
- 安装anaconda并配置环境
安装anaconda的步骤 1.确定系统信息 uname -a 2.下载对应版本 3.sh 安装shell脚本 4.添加到对应路径 5.安装完anaconda之后,创建虚拟环境 conda creat ...