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 ...
随机推荐
- Macbook之安装opencv
link:http://www.mobileway.net/2015/02/14/install-opencv-for-python-on-mac-os-x/
- 304 CORS
304响应, CORS问题: 没有 Access-Control-Allow-Origin 这个头信息时,以前次返回的200请求为准. 示例:可能已被删除 http://7af3zm.com1.z0. ...
- 跟我一起学习ASP.NET 4.5 MVC4.0(一)(转)
由于上面一个项目使用的是ASP.NET4.0 MVC3.0,在招人的时候发现很多人有听说过MVC,但是却是没用过,对MVC也只是一知半解,最近想给团队成员做一个系统的解说,让大家都可以学习一下ASP. ...
- win8.1开启虚拟wifi
1. 使用管理员身份打开cmd 2. 然后输入netsh wlan set hostednetwork mode=allow 3. 接着输入netsh wlan start hostednetwork ...
- [老老实实学WCF] 第五篇 再探通信--ClientBase
老老实实学WCF 第五篇 再探通信--ClientBase 在上一篇中,我们抛开了服务引用和元数据交换,在客户端中手动添加了元数据代码,并利用通道工厂ChannelFactory<>类创 ...
- OC5_@class关键字
// // ClassB.h // OC5_@class关键字 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zha ...
- PowerDesigner提示This data item is already used in a primary identifier.的处理
原文链接: http://blog.csdn.net/lopper/article/details/5293545 今天同事在编制一个数据库脚本的时候,提示了This data item is alr ...
- JavaScript写选项卡
方法一: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- 《CSS3秘笈》备忘录
第一部分 1. 类名称区分大小写:.special和.SPECIAL不一样 2. :focus 是通过单击或跳格集中在某个地方 3. ::selection 没有单冒号,被选中的文本[ 但是在I ...
- eclipse如何创建web项目
1. 打开eclipse,在File上New,然后选择Dynamic Web Project 2. 弹出的页面中如下图,在Project name中输入项目名称JavaWeb01,点击Next ...