题目:螺旋矩阵

难度: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. scrapy spider

    spider 定义:在spiders文件夹中由用户自定义,继承scrapy.Spider类或其子类 Spider并没有提供什么特殊的功能. 其仅仅请求给定的 start_urls/start_requ ...

  2. Windows Server 2016 下执行定时任务(英文系统)

    Step1. 找到“控制面板(Control Panel)” 打开 “管理工具(Administrative Tools)” Step2. 打开“任务计划(Task Schedule)” Step3. ...

  3. 分别用request和socket给百多发送请求

    1.方式1 import socket client = socket.socket() # 百度创建连接: 阻塞 client.connect(('www.baidu.com',80)) # 问百度 ...

  4. Android主页导航:fragment+viewpager

    简单实现Fragment+ViewPager实现主页导航控制,效果如下: 一.activity_main.xml布局文件: <?xml version="1.0" encod ...

  5. myeclipse如何删除自带Javaee里面jar包

    myeclipse是我们在使用Java开发时的一款不错的集成开发环境软件,一般在开发web项目的时候,都要引入相关的jar包,javaee包就是其中一个,有时候其里面的jar包可能与我们需要的不匹配, ...

  6. beego——模板处理

    beego的模板处理引擎采用的是Go内置的html/template包进行处理,而且beego的模板处理逻辑是采用了缓存编译方式, 也就是所有的模板会在beego应用启动的时候全部编译然后缓存在map ...

  7. xlrd,xlwt操作Excel实例

    把有合并单元格的信息读取出来,输出所在层数与位置 我要操作的Excel是这样的 要的到的是这样的效果 # -*- coding: utf-8 -*- import xlrd import xlwt r ...

  8. Django框架之单表操作

    一.添加表记录 对于单表有两种方式 # 添加数据的两种方式 # 方式一:实例化对象就是一条表记录 Frank_obj = models.Student(name ="海东",cou ...

  9. window.event.keycode值大全

    window.event.keycode值大全 event.keycode值大全 1 keycode 8 = BackSpace BackSpace 2 keycode 9 = Tab Tab 3 k ...

  10. zend studio设置utf8

    1. windows -> preference -> general -> workspace 2.项目右键 -> properities -> resource 3. ...