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

Tips:Input:一个m×n的二维数组,按照顺时针打印

解决办法:每一圈递归一次。

package medium;

import java.util.ArrayList;
import java.util.List; public class L54SpiralMatrix {
public List<Integer> spiralOrder(int[][] matrix) {
if (matrix == null)
return null;
List<Integer> ans = new ArrayList<>();
int rows = matrix.length;
if (rows==0) return ans;
int cols = matrix[0].length;
int row = 0;
List<Integer> ans1 = spiralOrderCore(matrix, 0, rows - 1, 0, cols - 1, ans);
return ans1;
} private List<Integer> spiralOrderCore(int[][] matrix, int row, int rows, int col, int cols, List<Integer> ans) {
if (row < rows && col < cols) {
for (int c = col; c < cols; c++) {
ans.add(matrix[row][c]);
}
for (int r = row; r < rows; r++) {
ans.add(matrix[r][cols]);
}
for (int i = cols; i > col; i--) {
ans.add(matrix[rows][i]);
}
for (int i = rows; i > row; i--) {
ans.add(matrix[i][col]);
}
spiralOrderCore(matrix, row + 1, rows - 1, col + 1, cols - 1, ans);
} else if (row == rows) {
for (int c = col; c <=cols; c++) {
ans.add(matrix[row][c]);
} } else if (col == cols) {
for (int r = row; r <= rows; r++) {
ans.add(matrix[r][col]);
}
} return ans;
} public static void main(String[] args) {
System.out.println("Main start");
int[][] matrix = { { 1, 2, 3 ,4}, { 5, 6,7, 8 }, { 9,10,11,12 } ,{13,14,15,16}};
int[][] ma={{2,3}};
int[][] matrix1 = { { 1, 2, 3}, {4, 5, 6},{7, 8 , 9}};
L54SpiralMatrix l54 = new L54SpiralMatrix();
List<Integer> ans = l54.spiralOrder(ma);
System.out.println("size:"+ans.size());
for (int i = 0; i <ans.size(); i++) {
System.out.println(ans.get(i));
} }
}

【leetcode】54.Spiral Matrix的更多相关文章

  1. 【LeetCode】54. Spiral Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...

  2. 【LeetCode】59. Spiral Matrix II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...

  3. 【leetcode】59.Spiral Matrix II

    Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...

  4. 【一天一道LeetCode】#54. Spiral Matrix

    一天一道LeetCode系列 (一)题目 Given a matrix of m x n elements (m rows, n columns), return all elements of th ...

  5. 【LeetCode】885. Spiral Matrix III 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【一天一道LeetCode】#59. Spiral Matrix II

    一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

  7. [Leetcode][Python]54: Spiral Matrix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...

  8. LeetCode OJ 54. Spiral Matrix

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  9. 【leetcode】867 - Transpose Matrix

    [题干描述] Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped ...

随机推荐

  1. git向码云上提交项目

    git向码云上提交项目 设置账号名字和邮箱 $ git config --global user.name "注册时账号的名字" $ git config --global use ...

  2. 【树形DP】洛谷1122_最大子树和

    又是一道树形DP的入门题,思想非常简单  然而我最开始还是存了两个状态[传送门] 题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课,路上 ...

  3. LeetCode 910. Smallest Range II

    很有意思的一道数学推理题目, 剪枝以后解法也很简洁.初看貌似需要把每个数跟其他数作比较.但排序以后可以发现情况大大简化:对于任一对元素a[i] < a[j], a[i] - k和a[j] + k ...

  4. 纪中OJ 2019.01.25【NOIP提高组】模拟 B 组 T2 数字对

    声明 数字对 Time Limits: 2000 ms    Memory Limits: 262144 KB Description 小 H 是个善于思考的学生,现在她又在思考一个有关序列的问题.  ...

  5. style.attr,currentStyle,getComputedStyle获取元素css

    老朋友object.style.attr 平常我们都是使用object.style.attr的方式来获取元素的属性, 但是这种方法具有很大的局限性——只能获取内联样式, 而下面的两种方法可以获取到元素 ...

  6. 浅谈style.height、clientHeight、offsetHeight、scrollHeight

    先分别介绍以下,以下资料来自MDN HTMLElement.offsetHeight 是一个只读属性,它返回该元素的像素高度,高度包含该元素的垂直内边距和边框,且是一个整数. Element.clie ...

  7. SQL SERVER循环遍历(普通循环和游标循环)

    1.首先需要一个测试表数据Student 2.普通循环 1)循环5次来修改学生表信息 --循环遍历修改记录--declare @i int   set @i=0while @i<5begin   ...

  8. 服务端调用接口API利器之HttpClient

    前言 之前有介绍过HttpClient作为爬虫的简单使用,那么今天在简单的介绍一下它的另一个用途:在服务端调用接口API进行交互.之所以整理这个呢,是因为前几天在测试云之家待办消息接口的时候,有使用云 ...

  9. 我们一起学习WCF 第八篇回调函数

    什么是回调函数? 一个简单的例子:小明想要在京东购买一件商品.他会登陆网站选好自己的商品.然后他把这件商品放在购物车,然后开始付钱(这个表示触发,不付钱不发货(排除货到付款)).然后京东的人员收到了小 ...

  10. Python接口测试实战4(上) - 接口测试框架实战

    如有任何学习问题,可以添加作者微信:lockingfree 课程目录 Python接口测试实战1(上)- 接口测试理论 Python接口测试实战1(下)- 接口测试工具的使用 Python接口测试实战 ...