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

思路:螺旋数组,须要控制输出方向。我的实现是定义一个boolean数组,与数组大小同样,数据初始全为false.然后定义一个int变量表示方向,碰到数据边界或者下一数据为true,则改变方向。直到越界或者所有为true。结束循环。

详细代码例如以下:

public class Solution {
public List<Integer> spiralOrder(int[][] a) {
List<Integer> list = new ArrayList<Integer>();
if(a.length == 0 || a[0].length == 0){
return list;
}
int i = 0;//行
int j = 0;//列
boolean[][] b = new boolean[a.length][a[0].length]; int o = 0;//表示方向,0:右;1:下;2:左。3:上 //在范围内循环。超出范围结束
while(i < a.length && i >= 0 && j < a[0].length && j >= 0){
if(b[i][j]){//假设已所有走完,结束循环
break;
} list.add(a[i][j]); //加入结果
b[i][j] = true;//已加入的标记为true,表示已经加入 switch(o){
case 0://往右走的方向
if(j == a[0].length - 1 || b[i][j+1]){
o = 1;//走到最右或者已标记,方向往下走
i++;
}else{
j++;
}
break; case 1:
if(i == a.length - 1 || b[i+1][j]){
o = 2;//走到最下或者已标记,方向往左走
j--;
}else{
i++;
}
break;
case 2:
if(j == 0 || b[i][j-1]){
o = 3;//走到最左或者已标记。方向往上走
i--;
}else{
j--;
}
break;
case 3:
if(i == 0 || b[i-1][j]){
o = 0;//走到最上或者已标记。方向往右走
j++;
}else{
i--;
}
break;
}
}
return list;
}
}

leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法的更多相关文章

  1. Leetcode 54:Spiral Matrix 螺旋矩阵

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

  2. [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 ...

  3. 【LeetCode】Spiral Matrix(螺旋矩阵)

    这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...

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

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

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

  6. LeetCode - 54. Spiral Matrix

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

  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] 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(Medium)

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

随机推荐

  1. Sqoop hive 和mysql 交互 完整案例

    本文完成:在hive里建管理表:注入部分数据:利用sqoop导入mysql中 期间:解决中文乱码问题   飞行报告故障表 建表命令 查看表 人工灌入少量数据 Windows系统向Linux系统数据传输 ...

  2. git常用命令和github

    工作区:就是你的工作目录 暂存区:它像个缓存区域,临时保存你的改动 版本区:就是你的git仓库 HEAD:相当于一个指针,指向你最近一次提交后的结果 git status 查看状态 git add . ...

  3. MySQL各种版本的下载方式

    1.在百度上搜“MySQL”,进入官网 原文地址:https://blog.csdn.net/mieleizhi0522/article/details/79109195

  4. FileWriter实现从一个文件中读取内容并写到另一个文件中

    FileWriter和FileOutputStream都是向文件写内容,区别是前台一次写一个字符,后者一次写一个字节 package com.janson.day20180827; import ja ...

  5. java一维数组的声明、初始化及排序

    public class TestArray { public static void main(String[] args) { /** 数组声明及动态初始化 int a[] = new int[a ...

  6. buf.writeDoubleBE()函数详解

    buf.writeDoubleBE(value, offset[, noAssert]) buf.writeDoubleLE(value, offset[, noAssert]) value {Num ...

  7. python字符串方法replace()简介

    今天写replace方法的时候的代码如下: message = "I really like dogs" message.replace('dog','cat') print(me ...

  8. Python学习:ModuleNotFoundError: No module named 'pygal.i18n' 的解决方法

    最近在学<Python编程:从入门到实践>,16.2小结中 from pygal.i18n import COUNTRIES 获取两个字母的国别码,我用的pygal的版本是2.4.0(终端 ...

  9. Batchelor Prize

    awards in fluid mechanics The Prize of $25,000 is awarded every four years to a single scientist for ...

  10. STM32F103移值FreeRtos笔记

    RTOS版本:FreeRTOS_V8.2.2 一.下载FreeRTOS源文件       这个可以在百度上下载,或者在官网上面下载http://www.freertos.org/a00104.html ...