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

思路: 可参考剑指offer:题20

class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
vector<int> vec;
if(!matrix.size() || !matrix[0].size()) return vec;
int row = matrix.size(), col = matrix[0].size();
int rl = 0, rh = row-1, cl = 0, ch = col-1;
while(rh >= rl && ch >= cl) {
for(int c = cl; c <= ch; ++c) vec.push_back(matrix[rl][c]);
if(++rl > rh) break;
for(int r = rl; r <= rh; ++r) vec.push_back(matrix[r][ch]);
if(--ch < cl) break;
for(int c = ch; c >= cl; --c) vec.push_back(matrix[rh][c]);
--rh;
for(int r = rh; r >= rl; --r) vec.push_back(matrix[r][cl]);
++cl;
}
return vec;
}
};

Spiral Matrix II

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

For example, Given n = 3,

You should return the following matrix:

[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int> > vec(n, vector<int>(n));
int rl = 0, rh = n-1, cl = 0, ch = n-1, v = 1;
while(rh >= rl && ch >= cl) {
for(int c = cl; c <= ch; ++c) vec[rl][c] = v++;
if(++rl > rh) break;
for(int r = rl; r <= rh; ++r) vec[r][ch] = v++;
if(--ch < cl) break;
for(int c = ch; c >= cl; --c) vec[rh][c] = v++;
--rh;
for(int r = rh; r >= rl; --r) vec[r][cl] = v++;
++cl;
}
return vec;
}
};

59. Spiral Matrix && Spiral Matrix II的更多相关文章

  1. leetcode@ [54/59] Spiral Matrix & Spiral Matrix II

    https://leetcode.com/problems/spiral-matrix/ Given a matrix of m x n elements (m rows, n columns), r ...

  2. Leetcode 74 and 240. Search a 2D matrix I and II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  3. Android图片旋转,缩放,位移,倾斜,对称完整示例(一)——imageView.setImageMatrix(matrix)和Matrix

    MainActivity如下: import android.os.Bundle; import android.view.MotionEvent; import android.view.View; ...

  4. Android图片旋转,缩放,位移,倾斜,对称完整演示样例(一)——imageView.setImageMatrix(matrix)和Matrix

    MainActivity例如以下: import android.os.Bundle; import android.view.MotionEvent; import android.view.Vie ...

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

  6. LeetCode(59):螺旋矩阵 II

    Medium! 题目描述: 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, ...

  7. 方差variance, 协方差covariance, 协方差矩阵covariance matrix | scatter matrix | weighted covariance | Eigenvalues and eigenvectors

    covariance, co本能的想到双变量,用于描述两个变量之间的关系. correlation,相关性,covariance标准化后就是correlation. covariance的定义: 期望 ...

  8. Directx Matrix.PerspectiveFovLH Matrix.PerspectiveFovRH的理解

    该函数一个四个参数public static Matrix PerspectiveFovLH ( float fieldOfViewY, float aspectRatio, float znearP ...

  9. HDU 2686 Matrix 3376 Matrix Again(费用流)

    HDU 2686 Matrix 题目链接 3376 Matrix Again 题目链接 题意:这两题是一样的,仅仅是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 ...

随机推荐

  1. Rhel6-mysql_cluster配置文档

    MySQL Cluster 是一种技术,其主要功能是在无共享的相关系统中部署内存中数据库 的 Cluster .在通过无共享体系结构,系统能够使用廉价的硬件,而且对软硬件无特殊要求. 此外,由于每个组 ...

  2. Linux I2C总线设备驱动模型分析(ov7740)

    1. 框架1.1 硬件协议简介1.2 驱动框架1.3 bus-drv-dev模型及写程序a. 设备的4种构建方法a.1 定义一个i2c_board_info, 里面有:名字, 设备地址 然后i2c_r ...

  3. OC中快速创建NSNumber NSDictionary NSArray的方法

    NSNumber: @()     @小括号 或者        NSNumber * num = @3;    NSValue * value = @4; NSDictionary :@{} @大括 ...

  4. Python的平凡之路(2)

    一.标准库(sys & os):   Python 的标准库(standard library) 是随着 Python 一起安装在你的电脑中的,是 Python 的一部分 (当然也有特殊情况. ...

  5. 关于javascript中的===和==

    =是赋值符号,==是等于,===是严格等于. 对于等号两边的数值,如果类型不相同会先转换类型再比较,===则不会转换类型. 例如3和“3”在==比较下true,在===下是false, null和un ...

  6. C和Objective-C的语法概要

    C语言的三个基本要素是数据.语句和函数,支持面向过程编程(POP). C语言有数据,数据分为常量和变量,数据的类型分为字符类型和数字类型,数字类型分为整数类型和浮点数类型,复合数据的类型有数组和结构, ...

  7. StringMisc

    //StringMisc.java // This program demonstrates the length, charAt and getChars // methods of the Str ...

  8. ExpandableListView二级列表

    package com.example.dajj; import android.os.Bundle;import android.app.Activity;import android.view.M ...

  9. map的应用

    1.map最基本的构造函数:    map<string , int >mapstring;         map<int ,string >mapint;    map&l ...

  10. CSS 样式的优先级

    1. 同一元素引用了多个样式时,排在后面的样式属性的优先级高 例如,下面的 div,同时引用了 [.default] 和 [.user] 中的样式,其中 [.user] 样式中的 width 属性会替 ...