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. SSH客户端

    Windows: winSCP SecureCRT Ubuntu: SecureCRT:安装方法http://www.phperstar.com/post/323

  2. 前台传到servlet的乱码问题要怎么处理

  3. 微信小程序个人理解

    1:小程序不是用HTML5开发,它是由微信全新定义的规范,是基于XML+JS的,不支持也不兼容HTML,兼容受限的部分CSS写法.(wxml) weixin markup language 2:小程序 ...

  4. Pure-ftpd无法连接到服务器 425错误

    今天是五一假期的前一天,闲来没事,打开自己的博客,发现很久没有备份数据了,由于工作方面的原因,自己慢慢的退出了技术界,但本人还是依然向往技术界啊!各位技术宅们,加油! 问题发现 当我打开FTP客户端软 ...

  5. ORACLE 对用户密码做限制

    1. 查看默认设置 2. PASSWORD_LIFE_TIME 60 --口令的生命周期,超过这段时间口令可能会自动过期,是否过期要看是否设定了PASSWORD_GRACE_TIME PASSWORD ...

  6. VIew中的触摸事件 touchBegin 等一系列方法

    5.触摸事件  touchBegin 等一系列方法 1)手指按下 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 2 ...

  7. XibDemo

    ////  MyviewViewController.h//  XibDemo////  Created by hehe on 15/9/21.//  Copyright (c) 2015年 wang ...

  8. iOS中—触摸事件详解及使用

    iOS中--触摸事件详解及使用 (一)初识 要想学好触摸事件,这第一部分的基础理论是必须要学会的,希望大家可以耐心看完. 1.基本概念: 触摸事件 是iOS事件中的一种事件类型,在iOS中按照事件划分 ...

  9. Silverlight引用WebService时取消对ServiceReferences.ClientConfig文件的依赖

    做过Silverlight项目的朋友都知道一般来说我们在Silverlight项目中都需要引用WebService或是WCF,引用的方式是在Visual Studio窗口中通过“添加服务引用”来添加引 ...

  10. Centos文本方式安装情况下lvm分区的创建

    作者:马 岩(Furzoom) (http://www.cnblogs.com/furzoom/)版权声明:本文的版权归作者与博客园共同所有.转载时请在明显地方注明本文的详细链接,未经作者同意请不要删 ...