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 ...
随机推荐
- 转: ios app架构设计
http://keeganlee.me/post/architecture/20160107 看完这一系列文章后就知道怎么回答这类问题了: App架构设计经验谈:接口的设计 App架构设计经验谈:技术 ...
- .Net (MVC) 随机生成验证码
以前一直对C#的GDI画图部分知识点不怎么用所以忘得差不多了,这两天正好公司要做一个博客系统,其中一个需求就是留言时为了防止恶意攻击必须填写验证码,正好借着这个机会复习了一下,以下是实现代码,写的比较 ...
- mvc的一些知识点
MVC是微软2009对外公布的第一个开源的表示层框架,这是微软的第一个开源项目, MVC目的不是取代WebFrom开发,只是web开发的另外一种选择 1.MVC设计模式 M:Model 主要是存储或者 ...
- Android Studio 1.3新版体验
Google发布的Android Studio最新版是 1.3 版,上周的I/O大会中三位Google工程师对Android Studio作了将近1小时的演讲: 之前一直习惯用Eclipse luna ...
- 运维人愿意听到的话 vs 不愿意听到的话
在公司兼做了接近两年的远程运维工作,与内部打交道的过程中听到各种各样的话, 简单摘列一下那些似曾相识的愿意.不愿意听到的话: ”先别操作,帮忙先拷贝日志!我们调查一下答复!“ vs ”你先重启一下 ...
- C#如何关闭一个窗口的同时打开另一个窗口
在.net的WinForm程序中,如果是直接起动的Form作为主窗口,那么这个主窗口是不能关闭的,因为它维护了一个Windows消息循环,它一旦关闭了就等于声明整个应用程序结束,所以新打开的窗口也就被 ...
- Xcode中如何更改Bundle identifier
1.如图所示,更改Info.plist 中的Bundle identifier
- ./configure:command not found 解决方法
有些下载下来的源码没有MAKEFILE文件,但是会有MAKEFILE.IN 和 configure, MAKEFILE文件则由后两个文件生成. 如果执行: $./configure 提示错误:./ ...
- redis setnx 分布式锁
private final String RedisLockKey = "RedLock"; private final long altTimeout = 1 * 60 * 60 ...
- 【转】理解依赖注入(IOC)和学习Unity
IOC:英文全称:Inversion of Control,中文名称:控制反转,它还有个名字叫依赖注入(Dependency Injection).作用:将各层的对象以松耦合的方式组织在一起,解耦,各 ...