Spiral Matrix(LintCode)
Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
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].
难得的一次AC! 虽然感觉题很水,但是像这样思路清晰的写下来,没有出任何的“小问题”,然后提交,AC的情况对我来说还是很少的。虽然代码依旧有些乱。
想法就是定义一个方向的二维数组,从(0,0)开始沿一个方向遍历,若碰壁(下个遍历目标越界或已被遍历)就一个不会碰壁的方向继续遍历;每一个方向都碰壁时结束。
public class Solution {
/**
* @param matrix a matrix of m x n elements
* @return an integer list
*/
int[][] f = new int[1000][1000];
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> list = new ArrayList<Integer>();
int m = matrix.length;
if(m == 0) return list;
int n = matrix[0].length;
int[][] d = new int[4][2];
d[0][0] = 0; d[0][1] = 1;
d[1][0] = 1; d[1][1] = 0;
d[2][0] = 0; d[2][1] = -1;
d[3][0] = -1; d[3][1] = 0;
int cd = 0;
int i = 0;
int j = 0;
list.add(matrix[0][0]);
f[0][0] = 1;
boolean flag = true;
while(flag) {
if(!isV(i,j,d[cd],m,n)){
int x = 0;
while(!isV(i,j,d[cd],m,n) && x < 4) {
cd = (cd + 1) % 4;
x++;
}
if(x >= 4) flag = false;
}else {
i += d[cd][0];
j += d[cd][1];
list.add(matrix[i][j]);
f[i][j] = 1;
}
}
return list;
}
boolean isV(int i ,int j ,int d[] ,int m ,int n) {
if(i + d[0] < 0 || i + d[0] >= m) return false;
if(j + d[1] < 0 || j + d[1] >= n) return false;
if(f[i + d[0]][j + d[1]] != 0)return false;
return true;
}
}
Spiral Matrix(LintCode)的更多相关文章
- [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& ...
随机推荐
- DataGridView导出到Word
#region 使用Interop.Word.dll将DataGridView导出到Word /// <summary> /// 使用Interop.Word.dll将DataGridVi ...
- leaflet一个前端gis框架,比openlayer更简单
leaflet一个前端gis框架,比openlayer更简单 作者github:https://github.com/mourner?tab=overview&from=2009-12-01& ...
- 【NOI】2004 郁闷的出纳员
[算法]平衡树(treap) [题解] treap知识见数据结构. 解法,具体细节见程序. #include<cstdio> #include<algorithm> #incl ...
- spring 那点事
Spring核心功能 DI(IOC) 何谓DI(IOC) DI(依赖注入)是spring的核心功能之一. Dependency Injection 和 Inversion of Control 其实就 ...
- 关于Redis在Linux手动安装配置
安装: 1.获取redis资源 wget http://download.redis.io/releases/redis-5.0.0.tar.gz 2.解压 tar xzvf redis-5.0.0. ...
- MapperScannerConfigurer不 property-placeholder
关于org.mybatis.spring.mapper.MapperScannerConfigurer不支持 property-placeholder 参考了http://www.oschina.ne ...
- 【shell】shell中各种括号的作用()、(())、[]、[[]]、{}
一.小括号,圆括号() 1.单小括号 () ①命令组.括号中的命令将会新开一个子shell顺序执行,所以括号中的变量不能够被脚本余下的部分使用.括号中多个命令之间用分号隔开,最后一个命令可以没有 ...
- mysql where/having区别
mysql> select 2-1 as a,password from mysql.user where user='root' having a>0; +---+----------- ...
- Perl6 Bailador框架(5):利用正则匹配路径
use v6; use Bailador; =begin pod 我们在路径设置上, 可以利正则表达式捕获的字符串作为子例程参数 =end pod get '/perl6/(.+)' => su ...
- linux内核启动分析(2)
-----以下内容为从网络上整理所得------ 主要介绍kernel_init线程(函数),这个线程在rest_init函数中被创建,kernel_init函数将完成设备驱动程序的初始化,并调用in ...