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 ...
随机推荐
- Unity基础-图形渲染
图形渲染-Camera Camera下的Clear Flags:Skybox,Don't Clear,Depth only(深度),Solid Color(固定颜色) Culling Mask:渲染层 ...
- HTML5一些特殊效果分享地址集合
页面预加载图片原生js: http://www.cnblogs.com/st-leslie/articles/5274568.html HTML5 FileReader读取本地文件: http://n ...
- PHP redis使用命令
很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/owlient/phpredis(支持redis 2.0.4) Redis::__constru ...
- json.dumps ensure_ascii 方法
在使用json.dumps时要注意一个问题 import json print (json.dumps('中国')) "\u4e2d\u56fd" 输出的会是 '中国' 中 ...
- FSMC原理通俗解释
所以不用GPIO口直接驱动液晶,是因为这种方法速度太慢,而FSMC是用来外接各种存储芯片的,所以其数据通信速度是比普通GPIO口要快得多的.TFT-LCD 驱动芯片的读写时序和SRAM的差不多,所以就 ...
- HDU - 1864 最大报销额 (背包)
题意: 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C类),要求每张发票的总额不得超过1000元,每张发票上,单项物品的价值不得超过600元.现请你编 ...
- (转)JVM各种内存溢出是否产生dump
对于java的内存溢出,如果配置-XX:+HeapDumpOnOutOfMemoryError,很明确的知道堆内存溢出时会生成dump文件.但永久代内存溢出不明确是否会生成,今天来做一个实验: 永久代 ...
- python单例模式的几种实现方法
单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. ...
- 在ie9下在textbox框里面输入内容按enter键会触发按钮的事件
问题 在ie下,如果存在有button标签,如果在textbox里面输入内容,按下enter键,则会触发第一个按钮的click事件,经过测试,在IE10以及以下的都存在这个问题 原因 浏览器默认行为不 ...
- 第五部分 linux 软件安装RPM SRPM与YUM
第五部分 linux 软件安装RPM SRPM与YUM 软件管理员简介 RPM与DPKG两大主流 rpm: redhat centos suse 命令:yum ...