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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [LeetCode 题解] Spiral Matrix

    前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目链接 54. Spiral Matrix ...

  4. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

  5. 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 ...

  6. [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 ...

  7. LeetCode 885. Spiral Matrix III

    原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...

  8. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

  9. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  10. 【leetcode】Spiral Matrix II (middle)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

随机推荐

  1. [BZOJ] 1441 Min

    题意:给一堆数ai,求S=Σxiai,使得S最小且为正整数 根据裴蜀定理,一定存在ax+by=gcd(a,b),同理可以推广到n个整数 也就是说,在不考虑正负的情况下,所有数的gcd就是所求 #inc ...

  2. 896. Monotonic Array

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  3. python基本操作(五)

    if 判断 if 条件: 代码1 代码2 代码3 代码块(同一缩进级别的代码,例如代码1.代码2和代码3是相同缩进的代码,这三个代码组合在一起就是一个代码块,相同缩进的代码会自上而下的运行) cls ...

  4. socketserver的使用

    socketserver底层也是使用线程实现的并发,直接上代码 # server import socketserver ''' socketserver使用模式: 1 功能类 class Myser ...

  5. Ubuntu 15 下 Qt 配置mysql链接及基本操作

    序 最近需要在Linux下做一个unix网络编程项目,选择了Ubuntu 最新版本15.04 : 开发环境:Qt 5 数据库: MySQL 安装Qt 和 MySQL 简要介绍一下软件的安装! 安装Qt ...

  6. stm32之ADC应用实例(单通道、多通道、基于DMA)

    文本仅做记录.. 硬件:STM32F103VCT6 开发工具:Keil uVision4 下载调试工具:ARM仿真器 网上资料很多,这里做一个详细的整合.(也不是很详细,但很通俗).  所用的芯片内嵌 ...

  7. Docker容器技术的核心原理

    目录 1 前言 2 docker容器技术 2.1 隔离:Namespace 2.2 限制:Cgroup 2.3 rootfs 2.4 镜像分层 3 docker容器与虚拟机的对比 1 前言 上图是百度 ...

  8. Linux学习-Linux的账号与群组

    使用者识别码: UID 与 GID Linux 主机并不会直接认识 你的"帐号名称"的,他仅认识 ID 啊 (ID 就是一组号码啦). 由于计算机仅认识 0 与 1,所 以主机对于 ...

  9. CDH4 journalnode方式手工安装手册之三

    一.                                启动JournalNode 每台机器都要执行: mkdir -p /smp/hadoop-cdh4/bch/ chmod -R 77 ...

  10. ROM,PROM,EPROM,EEPROM及FLASH存储器的区别

    在微机的发展初期,BIOS都存放在ROM(Read Only Memory,只读存储器)中.ROM内部的资料是在ROM的制造工序中,在工厂里用特殊的方法被烧录进去的,其中的内容只能读不能改,一旦烧录进 ...