题目:螺旋矩阵

难度: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. 区块链 block chain 去信任

    去中心化:不以参与交易的任何一方为中心 去信任:假定参与交易的任何一方都是不可信任的 区块链受到关注的原因 去中心化.去信任化.智能合约等,正好满足未来互联网持续发展所要求的信息的盖度自动化和高度程序 ...

  2. Cisco路由器DHCP配置浅析

    enable  config terminal (进入配置模式)  ip dhcp pool global(配置一个根地址池,global是地址池的名称,你可以采用有意义的字符串来表示) config ...

  3. Python迭代对象、迭代器、生成器

    在了解Python的数据结构时,容器(container).可迭代对象(iterable).迭代器(iterator).生成器(generator).列表/集合/字典推导式(list,set,dict ...

  4. 使用 Python 编写 vim 插件

    使用 Python 编写 vim 插件 - 技术翻译 - 开源中国社区 code {margin: 0;padding: 0;white-space: pre;border: none;backgro ...

  5. Python元组组成的列表转化为字典

    虽然元组.列表不可以直接转化为字典,但下面的确是可行的,因为经常用python从数据库中读出的是元组形式的数据. # 原始数据 rows = (('apollo', 'male', '164.jpeg ...

  6. 我的Android进阶之旅------>RxJava学习资料汇总

    在响应式编程中,应该牢记以下两点: everything is a stream(一切皆流) don't break the chain(不要打断链式结构) 记住,可观测序列就像一条河,它们是流动的. ...

  7. 小木虫emuch遭封禁,新域名muchong.com尚可用

    各位虫友: 因为小木虫站点个别虫子违反论坛规定,擅自上传了政治擦边的违规资源.导致小木虫域名 emuch.net 被通信局封禁! 我们第一时间对违规资源进行了删除处理,接下来.我们也将大批量的对站内的 ...

  8. PyNest——part 4: topologically structured networks

    part 4: topologically structured networks incorporating structure in networks of point neurons 如果我们使 ...

  9. java架构师之路:推荐的15本书

    作为Java程序员来说,最痛苦的事情莫过于可以选择的范围太广,可以读的书太多,往往容易无所适从.我想就我自己读过的技术书籍中挑选出来一些,按照学习的先后顺序,推荐给大家,特别是那些想不断提高自己技术水 ...

  10. Tensorflow 学习笔记(一)TensorFlow入门

    一.计算模型----计算图 1.1 计算图的概念:TensorFlow就是通过图的形式绘制出张量节点的计算过程,例如下图执行了一个a+b的操作. 1.2 计算图的使用 TensorFlow程序一般分为 ...