【LeetCode】Spiral Matrix
来源:互联网 发布:ubuntu postfixadmin 编辑:程序博客网 时间:2024/06/05 10:23
Spiral Matrix
Total Accepted: 9568 Total Submissions: 47010 My Submissions
Given 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、设定边界值,startx,endx,starty,endy。
2、左到右,上到下,右到左,下到上,直到while条件结束。
Total Accepted: 9568 Total Submissions: 47010 My Submissions
Given 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]
.【解题思路】
本题代码借鉴了别人的思路。
1、设定边界值,startx,endx,starty,endy。
2、左到右,上到下,右到左,下到上,直到while条件结束。
Java AC 340ms
public class Solution { public ArrayList<Integer> spiralOrder(int[][] matrix) {ArrayList<Integer> allList = new ArrayList<Integer>();if (matrix == null || matrix.length == 0) {return allList;}allList = printfMatrix(matrix);return allList;}public ArrayList<Integer> printfMatrix(int[][] matrix) {ArrayList<Integer> allList = new ArrayList<Integer>();int m = matrix.length;int n = matrix[0].length;int startx = 0;int starty = 0;int endx = m - 1;int endy = n - 1;while (startx <= endx && starty <= endy) {for (int i = starty; i <= endy; i++) {allList.add(matrix[startx][i]);}for (int i = startx+1; i <= endx; i++) {allList.add(matrix[i][endy]);}if (startx != endx) {for (int i = endy-1; i >= starty; i--) {allList.add(matrix[endx][i]);}}if (starty != endy) {for (int i = endx-1; i > startx; i--) {allList.add(matrix[i][starty]);}}startx++;endx--;starty++;endy--;}return allList;}}Python AC 76ms
class Solution: # @param matrix, a list of lists of integers # @return a list of integers def spiralOrder(self, matrix): list = [] if matrix is None or len(matrix) == 0: return list m = len(matrix) n = len(matrix[0]) startx = 0 starty = 0 endx = m - 1 endy = n - 1 while startx <= endx and starty <= endy: for i in range(starty, endy + 1): list.append(matrix[startx][i]) for i in range(startx + 1, endx + 1): list.append(matrix[i][endy]) if startx != endx: for i in range(endy - 1, starty - 1, -1): list.append(matrix[endx][i]) if starty != endy: for i in range(endx - 1, startx, -1): list.append(matrix[i][starty]) startx += 1 endx -= 1 starty += 1 endy -= 1 return list
0 0
- [leetcode]Spiral Matrix&&Spiral Matrix II
- Leetcode:Spiral Matrix与Spiral Matrix II
- LeetCode | Spiral Matrix /Spiral Matrix II
- 【Leetcode】【python】Spiral Matrix/Spiral Matrix II
- LeetCode: Spiral Matrix
- LeetCode: Spiral Matrix II
- LeetCode Spiral Matrix
- LeetCode Spiral Matrix II
- [Leetcode] Spiral Matrix
- [Leetcode] Spiral Matrix II
- LeetCode: Spiral Matrix
- LeetCode : Spiral Matrix II
- [LeetCode] Spiral Matrix II
- [LeetCode] Spiral Matrix
- leetcode 80: Spiral Matrix
- [Leetcode] Spiral Matrix
- [LeetCode]Spiral Matrix
- [LeetCode]Spiral Matrix II
- Linux USB Gadget--软件结构
- Bit Manipulation-计算一个整数中二进制中1的个数
- vtk 体绘制小结
- 向大师致敬---写在第9个非遗日
- 编程是不是21世纪的必备技能?
- 【LeetCode】Spiral Matrix
- NSAssert,NSCassert
- java 调用oracle自定义类型报错:无效的名称模式 user2.ARRAY_VAR
- easyui 的页面解析 parser解析器
- CDbCriteria使用总结
- DynamicAttributes接口
- 【甘道夫】HBase开发环境搭建过程中可能遇到的异常:No FileSystem for scheme: hdfs
- Java正则表达式应用
- C#下控制台程序窗口下启用快速编辑模式运行线程会阻止线程运行