• 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

  • 相关阅读:
    Illegal access: this web application instance has been stopped already. could not load **
    mysql 分配内存大小配置
    top 命令
    Linux 创建用户 限制SFTP用户只能访问某个目录
    curl get请求
    VMware虚拟机ubuntu显示屏幕太小解决办法
    Micropython TPYBoard V10X拼插编程实践之定时器 代码不精通?...
    Micropython TPYBoard v102 自动浇花实验
    Micropython TPYBoard读取芯片上的温度传感器
    TPYBoard v102 DIY照相机(视频和制作流程)
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4436371.html
Copyright © 2020-2023  润新知