leetcode-54. Spiral Matrix - Medium

descrition

GGiven 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:

小技巧(si,sj),(ei, ej),对角线可以唯一确定一个矩阵。循环获取结果,每次循环获取当前最外层的一圈数字。

  1. 从左到右,matrix[si][sj,...,ej]
  2. 从上到下,matrix[si,...,ej][ej]
  3. 从右到左,matrix[ei][ej-1,...,sj]
  4. 从下到上,matrix[ei-1,...,si-1][sj]

实现如代码所示,注意边界条件。

方法 2:

我们只需要模拟时钟的顺时针行走,每走一步获取一个元素,假设矩阵 matrix 有 m 行 n 列,那么只需要循环 m * n 次即可获取所有元素。算法描述如下:

定义两个辅助数组变量如下,dr 和 dc 分别表示行和列的方向变化。

dr={0, 1, 0, -1}
dc={1, 0, -1, 0}

两个数组共同组合有以下 4 种情况:

  1. dr[0], dc[0]:行的增量为 0,列的增量为 1,对应方法 1 的情况 1
  2. dr[1], dc[1]:行的增量为 1,列的增量为 0,对应方法 1 的情况 2
  3. dr[2], dc[2]:行的增量为 0,列的增量为 -1, 对应方法 1 的情况 3
  4. dr[3], dc[3]:行的增量为 -1,列的增量为 0,对应方法 1 的情况 4

我们总共遍历 m * n 次,每次获取一个元素,而没个元素的状态都将是以上 4 种状态中的一种。令 r,c 分别表示当前的行和列的下标,cr, cc 分别表示 candidate,即下一个候选位置的下标,di 表示当前的方向(对应以上 4 种情况,即 dr,dc 的下标),visited[][] 对应 matrix,表示元素是否被访问过。

对于任意一次遍历,若 r,c 合法,即没有超出矩阵边界又没有被访问过,那么我们可以获取当前元素。否则,我们需要调换方向,即更新 di 的值,具体实现参考代码。

实际上方法 2 不是最优解,相比于方法 1, 方法 2 需要一个辅助的数组,但其思想非常值得借鉴,尤其是状态控制的 dr[] 和 dc[] ,di

leetcode-solution

code

#include <iostream>
#include <vector>
#include <algorithm> using namespace std; class Solution{
public:
vector<int> spiralOrder(vector<vector<int> >& matrix){ //return spiralOrderCycle(matrix);
return spiralOrderSimilation(matrix); } // time-O(m*n), space-O(1) exclude the space used to return info.
vector<int> spiralOrderCycle(vector<vector<int> >& matrix){
vector<int> ans;
if(matrix.empty())
return ans; int rows = matrix.size(), cols = matrix[0].size();
int si = 0, sj = 0;
int ei = rows - 1, ej = cols - 1; while(si <= ei && sj<=ej){
spiralOrderOneCycle(matrix, si, sj, ei, ej, ans);
si++;
sj++;
ei--;
ej--;
} return ans;
} void spiralOrderOneCycle(vector<vector<int> >& matrix,
int si, int sj, int ei, int ej,
vector<int>& ans){
// top row
for(int j=sj; j<=ej; j++)
ans.push_back(matrix[si][j]); // right column
for(int i=si+1; i<=ei; i++)
ans.push_back(matrix[i][ej]); // down row if exist
if(si < ei){
for(int j=ej-1; j>=sj; j--)
ans.push_back(matrix[ei][j]);
} // left column if exist
if(sj < ej){
for(int i=ei-1; i>si; i--)
ans.push_back(matrix[i][sj]);
}
} // time-O(m*n), space=O(m*n)
vector<int> spiralOrderSimilation(vector<vector<int> > &matrix){
vector<int> ans;
if(matrix.empty())
return ans;
int rows = matrix.size(), cols = matrix[0].size();
vector<vector<bool> > visited(rows, vector<bool>(cols, false));
int dr[] = {0, 1, 0, -1};
int dc[] = {1, 0, -1, 0};
// start from the left-top corner (r,c) = (0,0) and from left to right
// di = 0, dr[0] and dc[0], correspond left to right.
int di = 0, r = 0, c = 0; for(int i=0; i<rows*cols; i++){
ans.push_back(matrix[r][c]);
visited[r][c] = true;
int cr = r + dr[di];
int cc = c + dc[di];
if(0 <= cr && cr < rows && 0 <= cc && cc < cols && !visited[cr][cc]){
r = cr;
c = cc;
}else{
// change direction
di = (di + 1) % 4;
r = r + dr[di];
c = c + dc[di];
}
} return ans;
}
}; int main()
{
return 0;
}

[array] leetcode - 54. Spiral Matrix - Medium的更多相关文章

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

  2. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

  3. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  4. Leetcode 54:Spiral Matrix 螺旋矩阵

    54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

  5. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  6. LeetCode: 54. Spiral Matrix(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...

  7. LeetCode 54. Spiral Matrix(螺旋矩阵)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  8. [leetcode]54. Spiral Matrix螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  9. [leetcode]54. Spiral Matrix二维数组螺旋取数

    import java.util.ArrayList; import java.util.List; /** * Given a matrix of m x n elements (m rows, n ...

随机推荐

  1. 1)C语言简介(C自考学习)

    C语言历史由来 世界上第一个高级语言是"ALFOL",而C的前身是ALGOL语言.1970年美国贝尔实验室的肯·汤普逊对BCPL(基本复合程序设计语言)进行了进一步的简化,突出了硬 ...

  2. TensorFlow简易学习[3]:实现神经网络

    TensorFlow本身是分布式机器学习框架,所以是基于深度学习的,前一篇TensorFlow简易学习[2]:实现线性回归对只一般算法的举例只是为说明TensorFlow的广泛性.本文将通过示例Ten ...

  3. head first python菜鸟学习笔记(第六章)

    1. Python提供字典,允许有效组织数据,将数据与名关联,从而实现快速查找,而不是以数字关联. 字典是内置数据结构,允许将数据与键而不是数字关联.这样可以使内存中的数据与实际数据的结构保持一致.? ...

  4. 【JDK1.8】JDK1.8集合源码阅读——TreeMap(二)

    一.前言 在前一篇博客中,我们对TreeMap的继承关系进行了分析,在这一篇里,我们将分析TreeMap的数据结构,深入理解它的排序能力是如何实现的.这一节要有一定的数据结构基础,在阅读下面的之前,推 ...

  5. 3721:和数-poj

    总时间限制:  1000ms 内存限制:  65536kB 描述 给定一个正整数序列,判断其中有多少个数,等于数列中其他两个数的和. 比如,对于数列1 2 3 4, 这个问题的答案就是2, 因为3 = ...

  6. Python学习一

    安装时遇到的问题 安装anaconda3.0到D盘之后,配置好两个环境变量:D:\anaconda和D:\anaconda\Scripts.发现在命令行中执行python指令可以,但conda指令却是 ...

  7. ShoneSharp语言(S#)的设计和使用介绍系列(2)— 掀开盖头

    ShoneSharp语言(S#)的设计和使用介绍 系列(2)- 掀开盖头 作者:Shone 声明:原创文章欢迎转载,但请注明出处,https://www.cnblogs.com/ShoneSharp. ...

  8. react入门到进阶(三)

    一.react样式 1.内联样式 在以前写html+css的时候,引入css的时候有一种方法就是内联,而在react中又有些不一样,样式是用变量的形式,如下 const styleComponentH ...

  9. Ceph luminous 安装配置

    Ceph luminous 安装配置 #环境centos7 , Ceph V12 openstack pike 与 ceph 集成 http://www.cnblogs.com/elvi/p/7897 ...

  10. Python中的选择排序

    选择排序 选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理如下.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大 ...