LeetCode_Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
注意几点: 1)spiral 总共转了几圈 2) 最后一圈的时候如果是“横”“竖”需要处理好边界
class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> res;
int rows = matrix.size();
if(rows == ) return res;
int columns = matrix[].size();
if( columns ==) return res;
int lays = rows > columns ? (columns+)/ : (rows+) /;
bool single = rows > columns ? columns% :rows% ;
for(int lay = ; lay < lays ; lay++)
{
int topRow = lay;
int rightColumn = columns - - lay;
//process the top row
for(int i = lay ; i <= rightColumn ; i++)
res.push_back(matrix[topRow][i]);
//process the right column ,not include the first of the right column element
for(int i = lay + ; i <= rows - - lay ; i++)
res.push_back(matrix[i][rightColumn]);
if(lay == lays - && single)
continue ;
//process the bottom row, not include the last of the bottom row element
for(int i = rightColumn - ;i >= lay ; i--)
res.push_back(matrix[rows--lay][i]);
//process the left;
for(int i = rows - - lay - ;i > lay ; i--)
res.push_back(matrix[i][lay]);
}
return res;
}
};
LeetCode_Spiral Matrix的更多相关文章
- LeetCode_Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation
今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:
- Pramp mock interview (4th practice): Matrix Spiral Print
March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...
- Atitit Data Matrix dm码的原理与特点
Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...
- Android笔记——Matrix
转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...
- 通过Matrix进行二维图形仿射变换
Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- KEIL 伪指令
//为了大家查找方便,命令按字母排序:0.ALTNAME 功能: 这一伪指令用来自定义名字,以替换源程序中原来的保留字,替换的保留字均可等效地用于子程序中. 格式: ALTNAME 保留字 自定义名 ...
- 超全超详细的HTTP状态码大全(推荐抓包工具HTTP Analyzer V6.5.3)
超全超详细的HTTP状态码大全 本部分余下的内容会详细地介绍 HTTP 1.1中的状态码.这些状态码被分为五大类: 100-199 用于指定客户端应相应的某些动作. 200-299 用于表示请求成功. ...
- DateTime字段控件值显示短格式的做法
后台取dateTime格式,前台格式化就好了 <input type="text" name="txtPartyTime" id="txtPar ...
- .net中除去IList中的多余项
IList<ActionInfo> tempList = new List<ActionInfo>(); IList<ActionInfo> tempActionL ...
- java实现矩阵连乘的动态规划
package com.cjs.algorithm; public class DynamicPlan { /** * 此方法用来求解矩阵连乘的最小数乘次数 * * @param p * 传入的要连乘 ...
- Bring it on
I am going to open a whole new English Blog here. Most blogs here would be computer technologies, in ...
- 将Maven项目转换成Eclipse支持的Java项目
当我们通过模版(比如最简单的maven-archetype-quikstart插件)生成了一个maven的项目结构时,如何将它转换成eclipse支持的java project呢? 1. 定位到mav ...
- hdu 1695 GCD(欧拉函数+容斥)
Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD( ...
- curl 学习
<?php // $username =13800138000; // $password =123456; // $sendto =13912345678; // $message = &qu ...
- VMware vSphere 5.5的12个更新亮点(2)
ACPI支持 以前版本的VMware虚拟机的局限性之一,是支持的虚拟设备数量甚少.vSphere 5.5引入了Virtual Hardware 10,这增加了基于SATA的虚拟设备节点,通过AHCI( ...