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. 单线程+多线程下载doutula.com图片

    现在是2018年8月11日11:26:42,我挖的eth又降价了..... 单线程 # -*- coding:utf-8 -*- import re import os import urllib i ...

  2. 时间戳Unix timestamp

    (1)定义 Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01 ...

  3. 团队展示网页 HTML模版

    之前帮着领导,参加了iGEM的校内赛的网页制作,一开始也是用的现成的模版,但后面修修改改几乎面目全非了- 这里分享一下自己的网站,可以用做团队展示的网页模版,文件在末尾,大家自行下载吧-- 这里贴两张 ...

  4. 20155318 《Java程序设计》实验五 (网络编程与安全)实验报告

    20155318 <Java程序设计>实验五 (网络编程与安全)实验报告 实验内容 了解计算机网络基础 掌握Java Socket编程 理解混合密码系统 掌握Java 密码技术相关API的 ...

  5. 【转载】C/C++杂记:深入虚表结构

    原文:C/C++杂记:深入虚表结构 1. 虚表与“虚函数表” 在“C/C++杂记:虚函数的实现的基本原理”一文中曾提到“虚函数表”的概念,只是为了便于理解,事实是:虚函数表并不真的独立存在,它只是虚表 ...

  6. 【LG2257】YY的GCD

    [LG2257]YY的GCD 题面 洛谷 题解 题目大意: 给定\(n,m\)求\(\sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j)为质数]\). 我们设\(f(x)=[x为 ...

  7. MySQL入门篇(三)之my.cnf配置文件详解【转】

    转自:https://www.cnblogs.com/panwenbin-logs/p/8360703.html #*** client options 相关选项 ***# #以下选项会被MySQL客 ...

  8. C++ OI图论 学习笔记(初步完结)

    矩阵图 使用矩阵图来存储有向图和无向图的信息,用无穷大表示两点之间不连通,用两点之间的距离来表示连通.无向图的矩阵图是关于主对角线对称的. 如图所示: 使用dfs和bfs对矩阵图进行遍历 多源最短路径 ...

  9. charles录制https请求

    之前一直用windows系统,抓包什么的都是用的fiddler或者wireshark,操作比较简单,扩展性也比较强,现在因为工作原因换了mac,在网上一直没有找到fiddler的mac版本,就只能切换 ...

  10. Unity中StopCoroutine不起作用怎么办

    1,只有StartCoroutine使用一个字符串方法名时才能用StopCoroutine(string CoroutineName)停用. 2, public Coroutine coroutine ...