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& ...
随机推荐
- UVA 11040 Add bricks in the wall
https://vjudge.net/problem/UVA-11040 找规律 #include<cstdio> using namespace std; ][]; int main() ...
- NOJ/HUST 1095 校赛 Just Go 线段树模板题
Description There is a river, which contains n stones from left to right. These stones are magic, ea ...
- 正则表达式实现将html文本转换为纯文本格式(将html字符串转换为纯文本方法)
Regex regex = new Regex("<.+?>", RegexOptions.IgnoreCase); string strOutput = regex. ...
- Katu Puzzle(POJ3678+2-SAT问题+tarjan缩点)
题目链接:http://poj.org/problem?id=3678 题目: 题意:给你a,b,c,op,op为逻辑运算符或.与.异或,使得a op b = c,让你判断这些运算符是否存在矛盾,不存 ...
- centos 挂载数据盘
第一.检查硬盘设备是否有数据盘 fdisk -l 第二.数据硬盘分区 fdisk /dev/vdb 第三.ext3格式化分区 mkfs.ext3 /dev/vdb1 第四.挂载新分区 A - 新建目录 ...
- ASP.NET 设置DropDownList的当前选项
1.通过显示字符Text DropDownList.Items.FindByText("你的值").Selected=true; 如果在设置之前,进行过设置,应用如下格式: Dro ...
- 小程序_改变switch组件的大小
微信开发文档中,switch能修改颜色,没有直接修改switch大小的属性.用一般控件height & width来修改宽高是没有用的. 使用如下方法: 在.wxss文件: .wx-switc ...
- H题 hdu 2520 我是菜鸟,我怕谁
题目大意:http://acm.hdu.edu.cn/showproblem.php?pid=2520 我是菜鸟,我怕谁 Time Limit: 2000/1000 MS (Java/Others) ...
- 土司论坛nc反弹神器使用方法
说明: PS:我本机是linux,因为没有服务器所以使用win7来演示.倘若你是windows可以在本机生成dll以后再放到服务器上面去执行dll即可反弹shell物理机ip:192.168.1.12 ...
- vuejs怎么在服务器部署?
通过npm run build 把生成的dist文件夹(不要上传文件夹)里的内容上传到http服务器上就可以通过 http来访问了,开发机上正常,上传以后 程序出现错误不能运行的原因99.99%的可能 ...