[leetcode]54. Spiral Matrix二维数组螺旋取数
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二维数组螺旋取数的更多相关文章
- MVC5中使用jQuery Post 二维数组和一维数组到Action
很久没有写了,最近在做一个MVC项目,这是我做的第一个MVC项目.之前可以说多MVC一点都不了解,今天把昨天遇到的一个问题记录下来.MVC大神就请飘过吧,跟我遇到同样问题的可以进来看看.遇到的第一个问 ...
- Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- [算法][LeetCode]Search a 2D Matrix——二维数组的二分查找
题目要求 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- LeetCode——Rotate Image(二维数组顺时针旋转90度)
问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...
- 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 ...
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- LeetCode: 54. Spiral Matrix(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...
随机推荐
- Codeforces Round #677 (Div. 3)
F. Zero Remainder Sum || dp #include <cstdio> #include <algorithm> #include <cstring& ...
- LeetCode 004 Median of Two Sorted Arrays
题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...
- 如何在word中插入代码
本文使用的是word2007,在网上查阅资料,可以使用如下方法: 1. 插入一个1行1列的表格,然后将代码写在里面,完成之后选中表格: 2. 将样式改为"HTML代码". 其实只是 ...
- IEEE754标准浮点数表示与舍入
原文地址:https://blog.fanscore.cn/p/26/ 友情提示:本文排版不太好,但内容简单,请耐心观看,总会搞懂的. 1. 定点数 对于一个无符号二进制小数,例如101.111,如果 ...
- java12(eclipse断点调试)
选择结构switch 1.格式: switch(整型数据){ case 值A:System.out.println("");break; case 值B:System.out.pr ...
- vue中监视对象内部变化的三种方法
一,对整个对象监视 watch:{ obj:{ handler(newV,oldV){ console.log('obj changed') }, deep: true,//深度遍历 immediat ...
- 【题解】「P6771」[USACO05MAR]Space Elevator 太空电梯
P6771 这是一道很明显的 dp 问题. 首先 dp 最重要的三要素是:动态表示.动态转移.初始状态. 只要这三个要素搞明白了,基本就能把这题做出来了. solution 让我们来看看这题的动态表示 ...
- TMOOC-1692-分西瓜
题目 描述 今天是阴历七月初五,首师大附中信息社团队员GDC的生日.GDC正在和SCX.WXY在首师大附中集训.他想给这两位兄弟买点什么庆祝生日,经过调查,GDC发现SCX和WXY都很喜欢吃西瓜,而且 ...
- 题解-CF101D Castle
题面 CF101D Castle 给一棵 \(n\) 个节点的带权树,求一种遍历方案,从 \(1\) 出发,每条边走两次,走过所有点,第一次经过每个节点的平均时间最小.输出这个平均时间. 数据范围:\ ...
- CF392B Tower of Hanoi
题目链接. Description 三塔汉诺塔问题,给一个 \(3 \times 3\) 的矩阵 \(t\),\(t_{i, j}\) 表示从 \(i\) 塔移动一个盘子到 \(j\) 塔的花费. 初 ...