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. numpy学习(一)

    numpy数据类型 # numpy创建对象,numpy创建的对象是n阶矩阵,类似python中列表的嵌套 nd = np.array([[1,2,3,4,5],[2,3,4,6,5]])nd 结果: ...

  2. 201621123080《Java程序设计》第1周学习总结

    作业01-Java基本概念 1. 本周学习总结 关键词: JDK.JAVA.编程.基础语法 概念之间的关系: JDK是JAVA的开发工具,学习JAVA的主要方法是大量编程,语法是JAVA的基础 2. ...

  3. 在物理机上,用U盘安装esxi虚拟化环境

    一般使用U盘安装centos镜像,可使用镜像刻录工具UltraISO,详细方法参照如下链接: https://jingyan.baidu.com/article/647f0115ee55ba7f214 ...

  4. LA 4094 WonderTeam 构造

    题意: 一共有\(n\)支队伍参加比赛,每两支队伍比赛两场,主客场各一场. 胜场得\(3\)分,平局得1分,败场不得分. 一支得分为\(p\)的队伍的排名\(=\)分数大于\(p\)的队伍数\(+1\ ...

  5. ogre3D学习基础9 -- 光源程序实例

    这一章练习一下光源的使用,光源分为三种:点光源,聚光源,有向光.具体内容前面说过,这里就不解释了. 继续在上一章的程序的基础上实现. 1.创建摄像机(Camera) createCamera()函数是 ...

  6. python - 读取配置文件

    # -*- coding:utf-8 -*- ''' @project: jiaxy @author: Jimmy @file: read_config.py @ide: PyCharm Commun ...

  7. jmeter返回的Unicode转换成utf8

    该问题通过查找资料借鉴前辈门的经验得到了解决,记录下来是为了后面能够用到 最近发现有些接口返回的时unicode类型的,如下图所示: 因此只需要在jmeter中添加后置处理器:BeanShell Po ...

  8. X86保护模式 八操作系统类指令

    X86保护模式  八操作系统类指令 通常在操作系统代码中使用,应用程序中不应用这些指令 指令分为三种:实模式指令,任何权级下使用的指令.实模式权级0下可执行的指令和仅在保护模式下执行的指令 一  实模 ...

  9. python集合、字符编码、bytes与二进制

    集合 用括号表示{ },可以包含多个元素,用逗号分割 用途 用于关系运算 集合特点 1.每个元素是不可变类型 2.没有重复的元素 3.无序 应用 1.set去重 set(names)的功能是将列表转换 ...

  10. [译]如何在迭代字典的过程中删除其中的某些item(Python)

    最好不要在迭代的过程中删除.你可以使用解析式和filter过滤. 比方说: {key:my_dict[key] for key in my_dict if key !="deleted&qu ...