[LeetCode]--54. Spiral Matrix

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

我想着把这个二维数组分成一层一层的环,然后一层一层的遍历,就能得到,而遍历环就是上边右边下边左边。就是要控制好循环结束之类的。

public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res = new ArrayList<Integer>();
        if (matrix == null || matrix.length == 0)
            return res;
        int size = matrix.length * matrix[0].length;
        int len = Math.min(matrix.length, matrix[0].length);
        for (int i = 0; i < len; i++) {
            if (res.size() == size)
                break;
            visitCircle(res, matrix, i);
        }
        return res;
    }

    public void visitCircle(List<Integer> list, int[][] a, int m) {
        int len = a[0].length, hlen = a.length;
        for (int i = m; i < len - m; i++) {
            list.add(a[m][i]);
        }
        for (int i = 1 + m; i < hlen - m; i++) {
            list.add(a[i][len - m - 1]);
        }
        if (hlen - 2 * m == 1 || len - 2 * m == 1)
            return;
        for (int i = len - 2 - m; i >= m; i--) {
            list.add(a[hlen - m - 1][i]);
        }
        for (int i = hlen - m - 2; i > m; i--) {
            list.add(a[i][m]);
        }
    }

还有一种方法,跟我这个其实是一个意思,但是他没有这样表达。

public ArrayList<Integer> spiralOrder(int[][] matrix) {
        ArrayList<Integer> rst = new ArrayList<Integer>();
        if (matrix == null || matrix.length == 0)
            return rst;

        int rows = matrix.length;
        int cols = matrix[0].length;
        int count = 0;
        while (count * 2 < rows && count * 2 < cols) {
            for (int i = count; i < cols - count; i++)
                rst.add(matrix[count][i]);

            for (int i = count + 1; i < rows - count; i++)
                rst.add(matrix[i][cols - count - 1]);

            if (rows - 2 * count == 1 || cols - 2 * count == 1)
                // if only one row /col remains
                break;

            for (int i = cols - count - 2; i >= count; i--)
                rst.add(matrix[rows - count - 1][i]);

            for (int i = rows - count - 2; i >= count + 1; i--)
                rst.add(matrix[i][count]);

            count++;
        }
        return rst;
    }
时间: 2024-09-20 06:34:30

[LeetCode]--54. Spiral Matrix的相关文章

[LeetCode]59.Spiral Matrix II

[题目] Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] [分析] 模拟 [代码] /**-------------------------

Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] C++实现代码:(注意奇数和偶数的不同) #include<iostream> #inclu

[LeetCode]73.Set Matrix Zeroes

[题目] Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple improvement uses O(m + n) space, but

Spiral Matrix

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]. 分析 举个例子自己从头到尾把数字列出来,很容易就

leetcode难度及面试频率

转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two Pointers 2 Add Two Numbers 3 4 linked list Two Pointers           Math 3 Longest Substring Without Repeating Characters 3 2 string Two Pointers      

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

[经典面试题]蛇形矩阵(螺旋矩阵)

[1.打印蛇形矩阵] 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]. [代码] /**----

LeetCode总结【转】

转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetcode.com的online judge中151道算法题目.除各个题目有特殊巧妙的解法以外,大部分题目都是经典的算法或者数据结构,因此做了如下小结,具体的解题思路可以搜索我的博客:LeetCode题解 题目 算法 数据结构 注意事项 Clone Graph BFS 哈希表 Word Ladder II

[LeetCode]74.Search a 2D Matrix

[题目] Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previo