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 ...
随机推荐
- Jquery插件(CKEditor)
描述 在html页面实现像word一样的编辑功能(可视化HTML编辑器) 解决方法 ckeditor插件官方网站 http://ckeditor.com/ 使用 1:去官方下载ckeditor插件,添 ...
- 代码研磨 Slim v3 (二)--app->run()
APP->run()代码如下: /** * Run application * * This method traverses the application middleware stac ...
- 【转】自己动手写SC语言编译器
自序 编译原理与技术的一整套理论在整个计算机科学领域占有相当重要的地位,学习它对程序设计人员有很大的帮助.我们考究历史会发现那些人人称颂的程序设 计大师都是编译领域的高手,像写出BASIC语言的BIL ...
- 百度编辑器ueditor前台代码高亮无法自动换行解决方法
这两天本站成功安装整合了百度编辑器ueditor,用着还挺不错,但是遇到了点小问题 问题描述: 在内容里面插入代码高亮显示,后台编辑器中是可以自动换行的,但是发表后,在前台查看,发现代码不能自动换 ...
- Linux中RM快速删除大量文件/文件夹方法
昨天遇到一个问题,在Linux中有一个文件夹里面含有大量的Cache文件(夹),数量级可能在百万级别,使用rm -rf ./* 删除时间慢到不可接受.Google了一下,查到了一种方法,试用了下确实比 ...
- c# js调用AjaxPro方法出错解析
公司的项目的框架中有一部分用到了AjaxPro这个方法,看到这个方法的我一脸懵逼,老老实实去百度了一下. AjaxPro是.NET平台下的一个回调式AJAX框架,使用简单,功能强大.顾名思义ajax, ...
- 微信公众号与HTML 5混合模式揭秘1——如何部署JSSDK
本文是连载JSSDK+H5的书,这里是第一篇揭秘————如何部署JSSDK 部署JSSDK不会太难,有时候需要一点后台知识,但也不是太难的那种,本节主要是用PHP作为后台参考语言,为了照顾初学者,把代 ...
- AR模式
AR模式 在ThinkPHP框架中,一共存在两种操作模式:ORM模式与AR模式 ORM模式:① 实例化模型 ② 创建数据对象组装数组 ③ 调用相关方法执行相关操作 AR模式:① 实例化模型 ② 把数据 ...
- JS函数式编程【译】2.1 函数式编程语言
- Tomcat找不到service.bat文件
说明:我们给客户做安装包,Tomcat我们设置了编码和端口,所以用绿色版的,同时又要注册成windows服务.但是bin下面没有service.bat文件(tomcat6.exe,tomcat6x.e ...