Spiral Matrix

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

这道题用的递归的思想,将最外面一层添加到result列表中,然后将剩下元素作为一个数组,做下一次递归,感觉有点土,晚点去搜点高大上的

 import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; //class ListNode {
// public int val;
// public ListNode next;
// ListNode(int x) {
// val = x;
// next = null;
// }
// } public class Solution {
List<Integer> result = new ArrayList<Integer>(); public List<Integer> spiralOrder(int[][] matrix) {
if(null == matrix || matrix.length == 0)
return result; else if(matrix.length == 1 && matrix[0].length == 1) //只有一个元素直接添加
result.add(matrix[0][0]);
else if(matrix[0].length == 1){ //竖条
for(int i = 0; i < matrix.length; i++){
result.add(matrix[i][0]); //直接添加
}
}
else if(matrix.length == 1){ //横条
for(int i = 0; i < matrix[0].length; i++){
result.add(matrix[0][i]);
}
}
else {
for(int i = 0; i < matrix[0].length; i++){ //添加第一排
result.add(matrix[0][i]);
}
for(int i = 1; i < matrix.length; i++){ //添加最后一竖
result.add(matrix[i][matrix[0].length - 1]);
}
for(int i = matrix[0].length - 2; i >= 0; i--){ //添加最后一排
result.add(matrix[matrix.length - 1][i]);
}
for(int i = matrix.length - 2; i >= 1;i--){ //添加第一排
result.add(matrix[i][0]);
}
if(matrix.length - 2 != 0 && matrix[0].length - 2 != 0){
int next[][] = new int[matrix.length - 2][matrix[0].length - 2];
for(int i = 1; i < matrix.length - 1; i++){
for(int j = 1; j < matrix[0].length - 1;j++){
next[i - 1][j - 1] = matrix[i][j];
}
}
spiralOrder(next); //递归求解下一个矩阵的值
} } return result;
}
}

Spiral Matrix的更多相关文章

  1. [LeetCode] Spiral Matrix II 螺旋矩阵之二

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  2. [LeetCode] Spiral Matrix 螺旋矩阵

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

  3. LeetCode - 54. Spiral Matrix

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

  4. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  5. 【leetcode】Spiral Matrix II (middle)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  6. 59. Spiral Matrix && Spiral Matrix II

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

  7. LeetCode:Spiral Matrix I II

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

  8. Java for LeetCode 059 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  9. Leetcode#59 Spiral Matrix II

    原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...

  10. 1105. Spiral Matrix (25)

    This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...

随机推荐

  1. Windows装机指南

    开发相关: Anaconda整合了很多python的dependency,方便使用

  2. MVC 菜鸟学习记录1

    Asp.Net MVC 模式是一种表现模式.它将web应用程序分成三个主要组件即:    Model.View.Controller M:Model 主要是存储或者是处理数据的组件 Model其实是实 ...

  3. JDBC学习笔记(一)

    public static void main(String[] args) { ResultSet rs = null; Statement stmt = null; Connection conn ...

  4. c 语言时间的输出和比较

    time_t The most basic representation of a date and time is the type time_t. The value of a time_t va ...

  5. javascript之基本包装类型(Boolean,Number,String)基础篇

    前几天整理了javascript中Array方面的知识,但是String中的一些方法多多少少和Array里的方法有些类似容易混淆,就顺便连同String所在的包装类一起整理一下,希望可以帮助到初学者, ...

  6. ionic2 页面加载时图片添加的问题

    使用ionic2创建项目时,在app文件夹下有图片目录img 在home中引用图片,但是不论是用ng-src或者是src,代码如下: <ion-list> <ion-slides c ...

  7. 常用的HTML标签

    文本样式标签 列表标签 有序列表 语法格式: <ol> <li></li> <li></li> <li></li> ...

  8. 济南学习 Day 4 T1 pm

    幸运数字(number)Time Limit:1000ms Memory Limit:64MB题目描述LYK 最近运气很差,例如在 NOIP 初赛中仅仅考了 90 分,刚刚卡进复赛,于是它决定使用一些 ...

  9. POJ 2287 Tian Ji -- The Horse Racing(贪心)

    题意:田忌和齐王有n匹马,进行n局比赛,每局比赛输者给胜者200,问田忌最多能得多少钱. 分析:如果田忌最下等的马比齐王最下等的马好,是没必要拿最下等的马和齐王最好的马比的.(最上等马同理) 因此,如 ...

  10. Android开发代码规范

    目录 1.命名基本原则  2.命名基本规范 2.1编程基本命名规范 2.2分类命名规范 3.分类命名规范 3.1基本数据类型命名规范 3.2控件命名规范 3.3变量命名规范 3.4整个项目的目录规范化 ...