54. Spiral Matrix

题目:

Given a matrix of m x n elements (m rows, ncolumns), 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].

链接:  http://leetcode.com/problems/spiral-matrix/

题解:

转圈打印矩阵,需要设置left, right, top和bot四个变量来控制, 注意每次增加行/列前要判断list所含元素是否小于矩阵的总元素数目。需要再想办法简化一下。

Time Complexity - O(mn), Space Complexity - O(1)

public class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res = new ArrayList<>();
        if(matrix == null || matrix.length == 0)
            return res;
        int rowNum = matrix.length, colNum = matrix[0].length; 
        int left = 0, right = colNum - 1, top = 0, bot = rowNum - 1;
        
        while(res.size() < rowNum * colNum) {
            for(int col = left; col <= right; col++)
                res.add(matrix[top][col]);
            top++;
            if(res.size() < rowNum * colNum) {
                for(int row = top; row <= bot; row++)
                    res.add(matrix[row][right]);
                right--;    
            }
            if(res.size() < rowNum * colNum) {
                for(int col = right; col >= left; col--)
                    res.add(matrix[bot][col]);
                bot--;
            }
            if(res.size() < rowNum * colNum) {
                for(int row = bot; row >= top; row--)
                    res.add(matrix[row][left]);
                left++;
            }
        }
        
        return res;
    }
}

 

二刷:

跟一刷一样,设置上下左右四边界,然后编写就可以了。

Java:

Time Complexity - O(mn), Space Complexity - O(1)

public class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res = new ArrayList<>();
        if (matrix == null || matrix.length == 0) {
            return res;
        }
        int rowNum = matrix.length, colNum = matrix[0].length;
        int top = 0, bot = matrix.length - 1, left = 0, right = colNum - 1;
        int elementsLeft = rowNum * colNum;
        while (elementsLeft >= 0) {
            if (elementsLeft >= 0) {
                for (int i = left; i <= right; i++) {
                    res.add(matrix[top][i]);
                    elementsLeft--;
                }
                top++;
            }
            if (elementsLeft >= 0) {
                for (int i = top; i <= bot; i++) {
                    res.add(matrix[i][right]);
                    elementsLeft--;
                }
                right--;
            }
            if (elementsLeft >= 0) {
                for (int i = right; i >= left; i--) {
                    res.add(matrix[bot][i]);
                    elementsLeft--;
                }
                bot--;
            }
            if (elementsLeft >= 0) {
                for (int i = bot; i >= top; i--) {
                    res.add(matrix[i][left]);
                    elementsLeft--;
                }
                left++;
            }
        }
        return res;
    }
}

 

三刷:

条件是total > 0就可以了,不需要">="。

Java:

public class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res = new ArrayList<>();
        if (matrix == null || matrix.length == 0) return res;
        int rowNum = matrix.length, colNum = matrix[0].length;
        int left = 0, top = 0, right = colNum - 1, bot = rowNum - 1;
        int total = rowNum * colNum;
        while (total > 0) {
            for (int i = left; i <= right; i++) {
                res.add(matrix[top][i]);
                total--;
            }
            top++;
            
            if (total > 0) {
                for (int i = top; i <= bot; i++) {
                    res.add(matrix[i][right]);
                    total--;
                }
                right--;
            }
            
            if (total > 0) {
                for (int i = right; i >= left; i--) {
                    res.add(matrix[bot][i]);
                    total--;
                }
                bot--;
            }
            
            if (total > 0) {
                for (int i = bot; i >= top; i--) {
                    res.add(matrix[i][left]);
                    total--;
                }
                left++;
            }
        }
        return res;
    }
}

 

 

Reference:

https://leetcode.com/discuss/12228/super-simple-and-easy-to-understand-solution

https://leetcode.com/discuss/62196/ac-python-32ms-solution

https://leetcode.com/discuss/46523/1-liner-in-python

 

posted @ 2015-04-17 23:40  YRB  阅读(474)  评论(0编辑  收藏  举报