leetcode 之Spiral Matrix I 和 II 解题思路
来源:互联网 发布:淘宝刷客究竟犯不犯法 编辑:程序博客网 时间:2024/06/04 18:51
题目如下:
Spiral Matrix
My SubmissionsGiven 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]
.
螺旋形状需要有四个方向,右,下,左,上,再依次进行。当在向右移动时,遇到边界或者后面一个已经打印过,则改变方向,向下;其它方向依次相同。因此需要有一个二维数组记录当前的位置是否打印过,还需要有一个变量记录已经打印了多少个,当打印的个数和二维数组总个数相同时,则停止打印。
代码如下:
public class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> list = new ArrayList<Integer>(); int nRows = matrix.length; if(nRows == 0) return list; int nCols = matrix[0].length; int total_Len = nRows * nCols;//数组总长度 int[][] used = new int[nRows][nCols];//用于判断matrix数组中的元素是否打印过 int current_DIR = 0;//当前方向 int i = 0; int j = 0; int len = 0; while(true){ if(len == total_Len){//如果当前打印长度和总长度相等,则停止打印 break; } switch(current_DIR){ case 0:{//0代表向右的方向 if(j == nCols || used[i][j] == 1){//改变方向的边界条件 j--; i++; current_DIR = 1; }else{ list.add(matrix[i][j]); used[i][j] = 1; len++; j++; } break; } case 1:{//1代表向下 if(i == nRows || used[i][j] == 1){ i--; j--; current_DIR = 2; }else{ list.add(matrix[i][j]); used[i][j] = 1; len++; i++; } break; } case 2:{//2代表向左 if(j == -1 || used[i][j] == 1){ j++; i--; current_DIR = 3; }else{ list.add(matrix[i][j]); used[i][j] = 1; len++; j--; } break; } case 3:{//3代表向上 if(i == -1 || used[i][j] == 1){ i++; j++; current_DIR = 0; }else{ list.add(matrix[i][j]); used[i][j] = 1; len++; i--; } } } } return list; }}
Spiral Matrix II
Total Accepted: 10563 Total Submissions: 34583My SubmissionsGiven an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3
,
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]
public class Solution { public int[][] generateMatrix(int n) { if(n == 0) return new int[0][0]; int[][] matrix = new int[n][n]; int len = 0; int total_Len = n * n; int current_Dir = 0; int[][] used = new int[n][n]; int i = 0, j = 0; while(true){ if(len == total_Len){ break; } switch(current_Dir){ case 0:{ if(j == n || used[i][j] == 1){ j--; i++; current_Dir = 1; }else{ matrix[i][j] = ++len; used[i][j] = 1; j++; } break; } case 1:{ if(i == n || used[i][j] == 1){ i--; j--; current_Dir = 2; }else{ matrix[i][j] = ++len; used[i][j] = 1; i++; } break; } case 2:{ if(j == -1 || used[i][j] == 1){ j++; i--; current_Dir = 3; }else{ matrix[i][j] = ++len; used[i][j] = 1; j--; } break; } case 3:{ if(i == -1 || used[i][j] == 1){ i++; j++; current_Dir = 0; }else{ matrix[i][j] = ++len; used[i][j] = 1; i--; } break; } } } return matrix; }}
0 0
- leetcode 之Spiral Matrix I 和 II 解题思路
- leetCode 59.Spiral Matrix II (螺旋矩阵II) 解题思路和方法
- leetcode之Spiral Matrix 和Spiral Matrix II
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
- leetcode之Pascal's Triangle I 和 II 解题思路
- [LeetCode]Spiral Matrix I & II & Rotate Matrix
- LeetCode之Spiral Matrix II
- LeetCode之Spiral Matrix II
- leetcode之Spiral Matrix II
- 【Leetcode】之Spiral Matrix II
- [leetcode] 59. Spiral Matrix II 解题报告
- 【LeetCode】59. Spiral Matrix II 解题报告
- [Leetcode] 59. Spiral Matrix II 解题报告
- LeetCode 59. Spiral Matrix II 解题报告
- leetcode解题方案--059--Spiral Matrix II
- LeetCode题解:Spiral Matrix I and II
- [LeetCode]-Spiral Matrix I&II 螺旋矩阵
- LeetCode - Spiral Matrix I && II - Frequent
- 4个小启示,给大脑洗洗澡!
- 有没有操作简单的PDF转换器
- hdu 3177 Crixalis's Equipment (贪心)
- {}如何利用B2B平台进行推广
- MONGODB 数据库文件读取的优化
- leetcode 之Spiral Matrix I 和 II 解题思路
- Web of Knowledge应用技巧
- {}如何进行博客推广
- UIColor & 颜色集
- ios8技术调研
- tomcat 6.1.18启动报错IOException while loading persisted sessions: java.io.EOFException
- 09 js继承
- poj 3625 Building Roads(图论:最小生成树)
- gridview使用总结