• [算法][LeetCode]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].

    分析

    举个例子自己从头到尾把数字列出来,很容易就找到规律了:
    假设一维数组的坐标为x,取值范围是xMin~xMax;二维数组的坐标为y,取值范围是yMin~yMax。(也就是数组表示为int[y][x])
    1. 从左到右,y=yMin,x: xMin->xMax,yMin++
    2. 从上到下,x=xMax,y: yMin->yMax,xMax--
    3. 从右到左,y=yMax,x: xMax->xMin,yMax--
    4. 从下到上,x=xMin,y: yMax->uMin,xMin++
    结束条件,xMin==xMax或者yMin==yMax
     
    还要要注意的地方:空数组的情况要处理。

    Java代码

    public static ArrayList<Integer> spiralOrder(int[][] matrix) {
        ArrayList<Integer> order = new ArrayList<Integer>(); 
        if (matrix.length == 0 || matrix[0].length == 0) return order;
        
        int xMin = 0;
        int yMin = 0;
        int xMax = matrix[0].length - 1;
        int yMax = matrix.length - 1;
    
        order.add(matrix[0][0]);
        
        int i = 0, j = 0;
        while (true) {
            while (i < xMax)    order.add(matrix[j][++i]);
            if (++yMin > yMax)    break;
            
            while (j < yMax)    order.add(matrix[++j][i]);
            if (xMin > --xMax)    break;
            
            while (i > xMin)    order.add(matrix[j][--i]);
            if (yMin > --yMax)    break;
            
            while (j > yMin)    order.add(matrix[--j][i]);
            if (++xMin > xMax)    break;
        }
        return order;
    }
  • 相关阅读:
    浅尝EffectiveCSharp_6
    浅尝EffectiveCSharp_7
    浅尝EffectiveCSharp_9
    CLR_via_C#.3rd 翻译[1.6 框架类库]
    浅尝EffectiveC#_11
    CLR_via_C#.3rd 翻译[1.9 与非托管代码的操作]
    wcf学习笔记_2(修改wcf配置文件)
    CLR_via_C#.3rd 翻译[1.4.2 不安全代码]
    CLR_via_C#.3rd 翻译[1.4.1 IL与验证]
    CLR_via_C#.3rd 翻译[1.8 通用语言规范]
  • 原文地址:https://www.cnblogs.com/hiddenfox/p/3399910.html
Copyright © 2020-2023  润新知