题目:螺旋矩阵

难度: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. Audit File Delete

    OPEN 4656 ACCESS   4663 AN OBJECT WAS DELETED 4660 CLOSE 4658 1537 = Delete 1538 = Read_CONTROL 1541 ...

  2. About AcitveDirectory EventLog

    参考微软文档整理的常用EVENTID: Account Logon Account Management Policy Change Event ID Event message 分類 類別 4670 ...

  3. 微信公众号 openId 支付 php中file_get_contents与curl性能比较分析

    w http://www.jb51.net/article/57238.htm

  4. Response 和 Request

    1. request 对象和 response 对象均由服务器创建. 2. 服务器处理请求的流程: 服务器每次收到请求时, 都会为这个请求开辟一个新的线程; 服务器会把客户端的请求数据封装到 requ ...

  5. 2015-03-22——js常用其它方法

    Function Function.prototype.method = function (name, func) {    this.prototype[name] = func;  //此时th ...

  6. Python 爬虫 学习一

    # coding: utf8 import requests from bs4 import BeautifulSoup PhotoName = 1 DATA = [] def save_img(ur ...

  7. MySQL数据库(1)- 数据库概述、MySQL的安装与配置、初始SQL语句、MySQL创建用户和授权

    一.数据库概述 1.什么是数据(Data) 描述事物的符号记录称为数据,描述事物的符号既可以是数字,也可以是文字.图片,图像.声音.语言等,数据由多种表现形式,它们都可以经过数字化后存入计算机. 在计 ...

  8. Linux学习笔记(12)linux文件目录与用户管理

    基本常用目录 1.文件及目录 1.1.文件/文件夹权限 用法: (1)    chgrp group_name dir_name/file_name, (2)    chown user_name:g ...

  9. python中unicode和str的组合

    python中unicode对象和str对象拼接在一起,会自动将str对象转换成unicode对象 即:a="aa" b=u"bb" c=a+b type(c) ...

  10. git log 查看提交历史

    文章出处http://blog.csdn.net/wh_19910525/article/details/7468549  git log  查看 提交历史 在提交了若干更新之后,又或者克隆了某个项目 ...