Spiral Matrix
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的更多相关文章
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [LeetCode] Spiral Matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 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 ...
- 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 ...
- 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 ...
- Leetcode#59 Spiral Matrix II
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
- 1105. Spiral Matrix (25)
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
随机推荐
- Smarty安装配置方法
下载最新的Smarty:http://smarty.php.net/ 解压后将目录中的libs目录重命名为smarty,复制到你的网站目录,同时在网站根目录下建立templates和templates ...
- Android Studio 1.3新版体验
Google发布的Android Studio最新版是 1.3 版,上周的I/O大会中三位Google工程师对Android Studio作了将近1小时的演讲: 之前一直习惯用Eclipse luna ...
- Part 32 Abstract classes in c#
- Linux下用freetds连接mssql中文乱码的问题【参考1】
由于工作原因我们需要通过php访问我们以前的Sql Server 2005数据,所以就有了这篇文章的诞生.废话就少说了,做程序设计的最不喜欢兜圈子了.用简介步骤说明问题,往下看.系统: Linux ...
- C# Ajax 手机发送短信验证码 校验验证码 菜鸟级别实现方法
1.Ajax请求处理页面: using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...
- Row_Number实现分页(适用SQL)
1:首先是 select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1 生成带序号的集合 2:再查询该集合的 第 1 ...
- BeanFactory容器的设计原理
XmlBeanFactory设计的类继承关系 1.BeanFactory接口提供了使用IoC容器的规范.在这个基础上,Spring还提供了符合这个IoC容器接口的一系列容器的实现供开发人员使用. 2. ...
- 《HTML5与CSS3基础教程》学习笔记 ——One Day
第一章 1. 邮箱地址的URL地址包括:mailto:+邮箱地址 2. ../表示向上走一级,开头直接使用/表示根目录 第三章 1. <header>: role = “ ...
- AndroidStudio中gradle异常:unexpected end of block data
原因:可能是Android buildTools版本不够高. 解决方法:打开build.gradle,将android中buildToolsVersion改为'20.0.0' (我使用的是gradle ...
- LINQ to XML(1)
LINQ to XML可以两种方式和XML配合使用.第一种方式是作为简化的XML操作API,第二种方式是使用LINQ查询工具.下面我使用的是第二种方式. 主要内容:用LINQ查询语句对XML文件里的数 ...