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& ...
随机推荐
- 2015/9/15 Python基础(12):模块和包
模块是用来组织 Python 代码的方法,而包则是用来组织模块的. 当代码量很大时,我们一般会把代码分成几个有组织的代码段,然后每个代码段之间有一定的联系.代码单之间是共享的,所以Python允许调入 ...
- javaScript 进阶篇
1.js 数组 创建数组的语法: a. var myarray= new Array(8); myarray[0]=1;等等 b.var myarray = new Array(66,8,47,59, ...
- 「6月雅礼集训 2017 Day4」寻找天哥
[题目大意] 给出$n$个三维向量,设当前向量长度为$L$,每次沿着向量等概率走$[0,L]$个长度.一个球每秒半径增加1个长度,直到覆盖位置,每秒耗能为球体积,求总耗能的期望. 设最后半径为R,那么 ...
- Cordova入门
创建你的第一个App 因为对接要对接酷音,实现h5跨平台调用客户端的保存和分享功能,所以学了下cordova的入门. 安装Cordova CLI Cordova命令行工具作为npm包分发. 安装cor ...
- Select 使用不当引发的core,你应该知道的
排查一个死机问题,搞了好几天时间,最终确定原因:最终确定问题原因,在此分享一下: 第一步:常规根据core文件查看栈信息,gdb –c core xxxx 如下rip不正确,指令地址错乱,栈信息已破坏 ...
- 利用certutil.exe 传文件
certutill.exe 在Windows 7 及其之后的所有Windows Server和Workstation版本均预装 1. Encode file: certutil -encode kk. ...
- 移动端测试===Android内存管理: 理解App的PSS
Android内存管理: 理解App的PSS 原文链接:http://www.littleeye.co/blog/2013/06/11/android-memory-management-unders ...
- spark 环境搭建坑
spark的新人会有什么坑 spark是一个以java为基础的,以Scala实现的,所以在你在安装指定版本的spark,需要检查你用的是对应spark使用什么版本的scala,可以通过spark-sh ...
- 3.Python3标准库--数据结构
(一)enum:枚举类型 import enum ''' enum模块定义了一个提供迭代和比较功能的枚举类型.可以用这个为值创建明确定义的符号,而不是使用字面量整数或字符串 ''' 1.创建枚举 im ...
- seq和{ }生成序列
基本用法 [root@C ~]# seq 5 1 2 3 4 5 [root@C ~]# echo {1..5} 1 2 3 4 5 #步进输出 [root@C ~]# seq 1 2 5 1 3 5 ...