题目:螺旋矩阵

难度:Medium

题目内容

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

翻译

给定一个m x n元素的矩阵(m行,n列),以顺时针螺旋顺序返回矩阵的所有元素。

Example 1:

Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]

Example 2:

Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]

我的思路:因为每一圈都是由四条边组成的,所以写一个while死循环,里面有四个for循环,每个for循环结束之后都将相应的指标减少,同时判断是否超标,超标就退出。

eg:

while (true) {

// 横着 for  add

// top ++

// top 是否小于bottom 是则break

。。。

我的代码:

     public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<Integer>();
if(matrix.length == 0 || matrix[0].length == 0) return res; int top = 0;
int bottom = matrix.length-1;
int left = 0;
int right = matrix[0].length-1; while(true){
for(int i = left; i <= right; i++) res.add(matrix[top][i]);
top++;
if(left > right || top > bottom) break; for(int i = top; i <= bottom; i++) res.add(matrix[i][right]);
right--;
if(left > right || top > bottom) break; for(int i = right; i >= left; i--) res.add(matrix[bottom][i]);
bottom--;
if(left > right || top > bottom) break; for(int i = bottom; i >= top; i--) res.add(matrix[i][left]);
left++;
if(left > right || top > bottom) break;
} return res;
}

我的复杂度:O(N * M)     行乘以列

编码过程中的问题

1、没有注意到当输入为空的数组的时候 right 和bottom都会取成负数,所以得加上判空。

答案代码

和我一样的。

LeetCode第[54]题(Java):Spiral Matrix的更多相关文章

  1. 【LeetCode每天一题】Spiral Matrix II(螺旋数组II)

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

  2. 【LeetCode每天一题】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第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  4. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  5. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  6. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  7. LeetCode第[73]题(Java):Set Matrix Zeroes(矩阵置0)

    题目:矩阵置0 难度:Easy 题目内容: Given a m x n matrix, if an element is 0, set its entire row and column to 0. ...

  8. 【LeetCode每天一题】Set Matrix Zeroes(设置0矩阵)

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Exampl ...

  9. LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

随机推荐

  1. 决策树ID3算法python实现 -- 《机器学习实战》

    from math import log import numpy as np import matplotlib.pyplot as plt import operator #计算给定数据集的香农熵 ...

  2. yum -y install epel-release

    EPEL - Fedora Project Wiki https://fedoraproject.org/wiki/EPEL

  3. linux运维 vi vim q 的间接注释

    w q  --不发生写的写,无增删效果. 点q后,再次执行 vi /var/www/share/w.php 仍然会‘ Found a swap file by the name "/var/ ...

  4. IO 流之字节流和转换流

    基本读取操作: InputStream(); OutputStream(); // 直接写入目的地中, 不需要 flush() 刷新 write(byte[] b); // 参数为 byte 数组 字 ...

  5. Linux基础命令(二)

    作业一:1) 新建用户natasha,uid为1000,gid为555,备注信息为“master” groupadd -g 555 netasha useradd -u 1000 -g netasha ...

  6. pdb

    core code: import pdb pdb.set_trace() 单步执行并进入:s 单步执行并不进入:n 下一断点:c 当前位置:where 从当前函数返回:r 退出:q pdb comm ...

  7. tornado下使用静态文件和文件缓存

    静态文件和文件缓存 1.在应用配置 settings 中指定 static_path 选项来提供静态文件服务:   2.在应用配置 settings 中指定 static_url_prefix 选项来 ...

  8. 基于mondrain 的原理纠正特殊指标值

    原文地址:http://www.cnblogs.com/qiaoyihang/p/7348385.html 下面有两张表 数学试卷成绩 表1 学号 省份 批次 学校 试卷成绩 数学试卷小题成绩 表2 ...

  9. DP专题·四(树形dp)

    1.poj 115 TELE 题意:一个树型网络上有n个结点,1~n-m为信号传送器,n-m+1~n为观众,当信号传送给观众后,观众会付费观看,每铺设一条道路需要一定费用.现在求以1为根,使得收到观众 ...

  10. java要注意的问题1

    一.优先返回空集合而非null 如果程序要返回一个不包含任何值的集合,确保返回的是空集合而不是null.这能节省大量的”if else”检查. public class getLocationName ...