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. wpf中UserControl的几种绑定方式

    我们经常会抽取一些可重用的控件,某个属性是否需要重用,直接决定了这个属性的绑定方式. 1.完全不可重用的控件 有一些与业务强相关的控件,它们的属性完全来自ViewModel,越是相对复杂的控件,越容易 ...

  2. Android - PopupWindow

    PopupWindow就是一个可以显示在当前界面上的浮动容器,它可以用来显示任意一个View,同时还可以指定显示的位置. 下面写一个显示下拉的例子: PopupWindow mpopupWindow; ...

  3. Markov Random Fields

    We have seen that directed graphical models specify a factorization of the joint distribution over a ...

  4. Python的平凡之路(3)

     一.函数基本语法及特性 面向对象:(华山派)—类 —class 面向过程:(少林派)—过程 —df 函数式编程:逍遥派    —函数— df 一般的,在一个变化过程中,如果有两个变量x和y,并且对于 ...

  5. php大力力 [040节] 买了一天域名,整了一天后台,新网后台不懂啊

    php大力力 [040节] 买了一天域名,整了一天后台,新网后台不懂啊]]] 还有万网那些域名要备案,备案,备案中...................wqnmlgb 今天摩托车的前后轮被扎了,tnn ...

  6. 简单poi读取excel

    1.添加依赖jar包 maven配置: <!-- poi being --> <dependency> <groupId>org.apache.poi</gr ...

  7. OpenSSL - RSA非对称加密实现

    非对称加密:即两端使用一对不同的密钥进行加密. 在非对称加密中,需要两对密钥,公钥和私钥. 公钥个私钥属于对立关系,一把加密后,只有另一把才可以进行解密. 公钥数据加密 数字证书内包含了公钥,在进行会 ...

  8. Node.js高级编程读书笔记 - 5 数据库 - Never

    Outline 6 连接数据库 6.1 使用node-mysql连接MySQL数据库 6.2 使用Nano连接CouchDB数据库 6.3 使用Mongoose连接MongoDB数据库 6 连接数据库 ...

  9. mysql 可能会用到的一些 函数

    DATE_SUB(sysdate(), INTERVAL 8 HOUR)  //当前时间前 8小时 ifnull("","")  Parameter //添加一 ...

  10. ES6 - for...of

    for...of是一种用来遍历数据结构的方法,可遍历的对象包括:数组,对象,字符串,节点数组等 我们先来看一下现在存在的遍历方式: var arr=[1,2,3,4] (1)for循环 缺点:代码不够 ...