Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

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].

难得的一次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)的更多相关文章

  1. [LeetCode] Spiral Matrix II 螺旋矩阵之二

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  2. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  3. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

  4. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  5. 【leetcode】Spiral Matrix II (middle)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Leetcode#59 Spiral Matrix II

    原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...

随机推荐

  1. UVA 12034 Race

    https://vjudge.net/problem/UVA-12034 题意:n个人比赛,有多少种可能的结果 假设i个人中,有j个人是第一名,方案数为C(i,j) 所以ans=Σ C(n,j)* f ...

  2. HDU 2639 Bone Collector II (dp)

    题目链接 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in ...

  3. 获取子iframe框架的元素

    我们常常遇到使用iframe框的时候,该iframe框不能根据自己内部的内容撑起来的这种问题 必要条件:不能在跨域的情况下...本地可以放到localhost下进行测试 //父页面index.html ...

  4. vue手势解决方案

    1.需求 因为项目中要做一个可以移动.旋转和放缩具有合成图片的功能,例如: 剑可以随意移动,然后把位移.旋转角度和放缩值传给后台进行合成. 2.解决方案 网上搜到手势插件AlloyFinger,htt ...

  5. Android Studio 中引入Library

    启动AndroidStudio后,打开你需要接收Library的项目.比如有两个项目,项目A,和Library项目B,那么打开项目A.图中所示为项目的结构图,点击右上角的File菜单. 2 在下拉菜单 ...

  6. Django rest framework + Vue简单示例

    构建vue项目参考这篇文章https://segmentfault.com/a/1190000008049815 一.创建Vue项目 修改源:npm config set registry https ...

  7. 大原則 研讀 spec 與 code 的 心得

    最近在研究 stm32f429i-disc0 的 device tree source code, 並且 參造 Devicetree Specification Release 0.1, 在 dts ...

  8. nodejs 使用redis 管理session

    一.在开发机安装redis并远程连接 因本人的远程开发机配置原因,使用jumbo安装redis 首先登录开发机,并使用jumbo 安装redis:jumbo install redis 查看redis ...

  9. C 封装一个通用链表 和 一个简单字符串开发库

    引言 这里需要分享的是一个 简单字符串库和 链表的基库,代码也许用到特定技巧.有时候回想一下, 如果我读书的时候有人告诉我这些关于C开发的积淀, 那么会走的多直啊.刚参加工作的时候做桌面开发, 服务是 ...

  10. Kettle提高输入输出数据总结

    1 mysql在数据连接是可以通过设置一下三个三处的方式 useServerPrepStmts=false       useCursorFetch=true      useCompression= ...