Middle-题目111:54. Spiral Matrix

原创 2016年05月31日 20:03:07

题目原文:
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].
题目大意:
按顺时针螺旋顺序遍历二维数组。
题目分析:
Middle-题目29很像,在前面题基础上简单改一下代码即可。一个是读数组一个是写数组,所以读数组要使用一个visited[][]数组维护是否读过,而写数组只要判断是否为0即可。
源码:(language:java)

public class Solution {
    private  final int RIGHT = 1;
    private  final int DOWN = 2;
    private  final int LEFT = 3;
    private  final int UP = 4;
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> list = new ArrayList<Integer>();
        int row = matrix.length;
        if(row==0)
            return list;
        int col = matrix[0].length;
        boolean[][] visited = new boolean[row][col];
        int count = 0,direction = RIGHT,i=0,j=0;
        while (count < row * col) {
            list.add(matrix[i][j]);
            visited[i][j] = true;
            switch (direction) {
                case RIGHT:
                    if (j==col-1 || visited[i][j+1]) {
                        direction=DOWN;
                        i++;
                    }
                    else 
                        j++;
                    break;
                case DOWN:
                    if (i==row-1 || visited[i+1][j]) {
                        direction = LEFT;
                        j--;
                    }
                    else
                        i++;
                    break;
                case LEFT:
                    if (j==0 || visited[i][j-1]) {
                        direction = UP;
                        i--;
                    }
                    else
                        j--;
                    break;
                case UP:
                    if (visited[i-1][j]) {
                        direction = RIGHT;
                        j++;
                    }
                    else
                        i--;
                    break;
                default:
                    break;
            }
            count++;
        }
        return list;
    }
}

成绩:
1ms,beats 3.18%,众数1ms,71.07%

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

[C++]LeetCode: 111 Spiral Matrix II (螺旋写入矩阵)

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

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

一、问题描述 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...

leetcode-java-54. Spiral Matrix

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

(java)leetcode-54:Spiral Matrix

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the ma...

LeetCode#54. Spiral Matrix

按照Spider Order遍历数组:一个二维数组瞬时间绕圈走,将经过的数组元素以List返回

[LeetCode]54.Spiral Matrix

【题目】 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in...

Leetcode 54. Spiral Matrix

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

LeetCode54/59 Spiral Matrix I/II

一:Spiral Matrix I 题目: Given a matrix of m x n elements (m rows, n columns), return all elements ...

[LeetCode]--54. Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)