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. [ 1, 2, 3 ],
  3. [ 4, 5, 6 ],
  4. [ 7, 8, 9 ]
  5. ]

You should return [1,2,3,6,9,8,7,4,5].

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

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

  1. package medium;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class L54SpiralMatrix {
  7. public List<Integer> spiralOrder(int[][] matrix) {
  8. if (matrix == null)
  9. return null;
  10. List<Integer> ans = new ArrayList<>();
  11. int rows = matrix.length;
  12. if (rows==0) return ans;
  13. int cols = matrix[0].length;
  14. int row = 0;
  15. List<Integer> ans1 = spiralOrderCore(matrix, 0, rows - 1, 0, cols - 1, ans);
  16. return ans1;
  17. }
  18.  
  19. private List<Integer> spiralOrderCore(int[][] matrix, int row, int rows, int col, int cols, List<Integer> ans) {
  20. if (row < rows && col < cols) {
  21. for (int c = col; c < cols; c++) {
  22. ans.add(matrix[row][c]);
  23. }
  24. for (int r = row; r < rows; r++) {
  25. ans.add(matrix[r][cols]);
  26. }
  27. for (int i = cols; i > col; i--) {
  28. ans.add(matrix[rows][i]);
  29. }
  30. for (int i = rows; i > row; i--) {
  31. ans.add(matrix[i][col]);
  32. }
  33. spiralOrderCore(matrix, row + 1, rows - 1, col + 1, cols - 1, ans);
  34. } else if (row == rows) {
  35. for (int c = col; c <=cols; c++) {
  36. ans.add(matrix[row][c]);
  37. }
  38.  
  39. } else if (col == cols) {
  40. for (int r = row; r <= rows; r++) {
  41. ans.add(matrix[r][col]);
  42. }
  43. }
  44.  
  45. return ans;
  46. }
  47.  
  48. public static void main(String[] args) {
  49. System.out.println("Main start");
  50. int[][] matrix = { { 1, 2, 3 ,4}, { 5, 6,7, 8 }, { 9,10,11,12 } ,{13,14,15,16}};
  51. int[][] ma={{2,3}};
  52. int[][] matrix1 = { { 1, 2, 3}, {4, 5, 6},{7, 8 , 9}};
  53. L54SpiralMatrix l54 = new L54SpiralMatrix();
  54. List<Integer> ans = l54.spiralOrder(ma);
  55. System.out.println("size:"+ans.size());
  56. for (int i = 0; i <ans.size(); i++) {
  57. System.out.println(ans.get(i));
  58. }
  59.  
  60. }
  61. }

【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. R语言学习笔记(二十四):plyr包的用法

    plyr 这个包,提供了一组规范的数据结构转换形式. Input/Output list data frame array list llply() ldply() laply() data fram ...

  2. 【LG4585】[FJOI2015]火星商店问题

    [LG4585][FJOI2015]火星商店问题 题面 bzoj权限题 洛谷 \(Notice:\) 关于题面的几个比较坑的地方: "一天"不是一个操作,而是有0操作就相当于一天开 ...

  3. python 内置模块(sys)

    sys.argv           命令行参数List,第一个元素是程序本身路径sys.exit(n)        退出程序,正常退出时exit(0)sys.version        获取Py ...

  4. 【Windows定时关机】windows实现定时关机与取消

    背景:本人昨晚本来打算将电脑设置为晚上12点 30定时关机,结果写成了:12:30,所以就在刚才,我正玩游戏的时候, 电脑弹出提示:“windows将在一分钟内关闭”,我刚开始一脸懵逼,后来打开昨天敲 ...

  5. 【转】linux下,如何把整个文件夹上传到服务器(另一台linux)

    原文转自:https://zhidao.baidu.com/question/1046040541327493019.html 1.Linux下目录复制:本机->远程服务器 scp  -r /h ...

  6. Python运维三十六式:用Python写一个简单的监控系统

    市面上有很多开源的监控系统:Cacti.Nagios.Zabbix.感觉都不符合我的需求,为什么不自己做一个呢? 用Python两个小时徒手撸了一个简易的监控系统,给大家分享一下,希望能对大家有所启发 ...

  7. xshell连接虚拟机linux系统失败问题

    问题:在xshell新建对话弹出的对话框中输入ip地址后,确定并没有弹出输入用户名和密码对话框 直接显示连接失败 Could not connect to ): Connection failed. ...

  8. 二叉树的宽度<java版>

    二叉树的宽度 思路:层序遍历的时候,记录每层的节点数量,最后取记录中的最多的数量. 代码实现: public int solution(TreeNode node){ LinkedList<Tr ...

  9. python-python爬取妹子图片

    # -*- conding=utf-8 -*- import requests from bs4 import BeautifulSoup import io url = "https:// ...

  10. 实现短信超链接调起APP

    因APP推广的需求,需要给APP用户定期发送短信提醒登录使用,为了更好的用户体验在短信内容中嵌入了可以直接打开APP的超链接,下面介绍一下具体的代码实现. 编辑openApp.html文件: < ...