import java.util.ArrayList;
import java.util.List; /**
* 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].
二维数组的螺旋取数,思路和翻转二维数组差不多,分层然后每层取数
难点是判断层数是根据短边确定,每层取数时上下左右四行(列)的坐标确定,注意长和宽(m和n)分清楚
还有一个难点是如果短边是一个奇数,那么最后一层(即层数是短边/2(层数从0开始)时)是取不到的,要单独去判断,
分情况(最后一行是竖着还是横着)进行取数。
写代码时出错的地方:
1.判断层数一开始用长边计算的
2.忘了考虑输入是空数组的情况
3.没考虑短边是奇数时,最后一层取不到
二维数组分层,每条边的坐标(顺时针取)可以记住:
i为层数,j为每层的循环数,m是行数,n是列数
上边:(i,i+j)
右边:(i+j,n-1-i)
下边:(m-1-i,n-1-i-j)
左边:(n-1-i-j,i)
*/
public class Q54SpiralMatrix {
public static void main(String[] args) {
int[][] matrix = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
System.out.println(spiralOrder(matrix));
}
public static List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
int m = matrix.length;
if(m == 0)
return res;
int n = matrix[0].length;
int l = Math.min(m,n);
int k = l/2;
for(int i = 0;i < k;i++)
{
for (int j = 0; j < n-(i*2)-1; j++) {
res.add(matrix[i][i+j]);
}
for (int j = 0; j < m-(i*2)-1; j++) {
res.add(matrix[i+j][n-1-i]);
}
for (int j = 0; j < n-(i*2)-1; j++) {
res.add(matrix[m-1-i][n-1-i-j]);
}
for (int j = 0; j < m-(i*2)-1; j++) {
res.add(matrix[m-1-i-j][i]);
}
}
if (l%2 != 0)
{
if (l == m)
{
for (int j = 0; j <= n-(k*2)-1; j++) {
res.add(matrix[k][k+j]);
}
}
else
{
for (int j = 0; j <= m-(k*2)-1; j++) {
res.add(matrix[k+j][n-1-k]);
}
}
}
return res;
}
}

[leetcode]54. Spiral Matrix二维数组螺旋取数的更多相关文章

  1. MVC5中使用jQuery Post 二维数组和一维数组到Action

    很久没有写了,最近在做一个MVC项目,这是我做的第一个MVC项目.之前可以说多MVC一点都不了解,今天把昨天遇到的一个问题记录下来.MVC大神就请飘过吧,跟我遇到同样问题的可以进来看看.遇到的第一个问 ...

  2. Leetcode 54:Spiral Matrix 螺旋矩阵

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

  3. [算法][LeetCode]Search a 2D Matrix——二维数组的二分查找

    题目要求 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the ...

  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

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

  6. LeetCode——Rotate Image(二维数组顺时针旋转90度)

      问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...

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

  8. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  9. LeetCode: 54. Spiral Matrix(Medium)

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

随机推荐

  1. LeetCode 024 Swap Nodes in Pairs

    题目描述:Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  2. 小样本元学习综述:A Concise Review of Recent Few-shot Meta-learning Methods

    原文链接 小样本学习与智能前沿 . 在这个公众号后台回复'CRR-FMM',即可获得电子资源. 1 Introduction In this short communication, we prese ...

  3. 分享篇:聊一聊 15.5K 的 FileSaver,是如何工作的?

    聊一聊 15.5K 的 FileSaver,是如何工作的? FileSaver.js 是在客户端保存文件的解决方案,非常适合在客户端上生成文件的 Web 应用程序.它简单易用且兼容大多数浏览器,被作为 ...

  4. 第3.5节 丰富的Python字典操作

    一. 基本概念 Python提供一种通过名称来访问其各个值的数据结构,这种数据结构称为映射(mapping).字典(dict)是Python中唯一的内置映射类型,其中的值不按顺序排列,而是存储在键下, ...

  5. 第1章 Python学习环境构建目录

    第1章 引子 第1.1节 学习环境搭建 第1.2节 Python学习环境的使用 第2章 Python编程基础知识 第2.1节 简单的Python数据类型.变量赋值及输入输出 第2.2节 Python的 ...

  6. PyQt(Python+Qt)学习随笔:QTreeWidgetItem项的子项排序sortChildren及获取项对应的树型部件对象方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 1.sortChildren对子项排序 树型部件QTreeWidget中的QTreeWidgetIt ...

  7. 第11.22节 Python 中re模块的字符串分割器:split函数

    一. 引言 在<第11.2节 Python 正则表达式支持函数概览>介绍了re模块的主要函数,在<第11.3节 Python正则表达式搜索支持函数search.match.fullm ...

  8. PyQt(Python+Qt)学习随笔:QListWidget获取指定行对应项的item()方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 在列表部件中,可以通过item方法获取指定行对应的项,语法如下: QListWidgetItem i ...

  9. CF1407D Discrete Centrifugal Jumps 题解

    蒟蒻语 写了 \(100\) 行的 线段树上ST表维护二分维护单调栈维护dp, 结果最后发现只要俩单调栈就好了 = = 蒟蒻解 首先 \(dp_i\) 表示从 \(1\) 楼到 \(i\) 楼要跳几次 ...

  10. AcWing 406. 放置机器人

    大型补档计划 题目链接 预处理每个列.行连续块. 每个每个列行只能在一个位置匹配,否则冲突. 符合二分图性质,跑匈牙利即可. 点数最坏情况 \(N * M\) (墙空地相间分布),边数最坏情况 \(N ...