美文网首页
54+59、Spiral Matrix、Spiral Matri

54+59、Spiral Matrix、Spiral Matri

作者: 小鲜贝 | 来源:发表于2018-04-18 19:08 被阅读0次

Spiral Matrix

Example

Given the following matrix:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
return [1,2,3,6,9,8,7,4,5].

思路
螺旋遍历输出一个m * n的数组。方向是上、右、下、左,依次循环。

解法

public class Solution{
    public static List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> result = new ArrayList<>();
        int m = matrix.length;// rows
        if (m == 0) {
            return result;
        }
        int n = matrix[0].length;// columns
        int topOffset = 0;
        int rightOffset = 0;
        int bottomOffset = 0;
        int leftOffset = 0;
        while (true) {
            // top
            for (int i = leftOffset; i < n - rightOffset; i++) {
                result.add(matrix[topOffset][i]);
            }
            topOffset++;
            if (topOffset + bottomOffset == m) {
                break;
            }
            // right
            for (int i = topOffset; i < m - bottomOffset; i++) {
                result.add(matrix[i][n - 1 - rightOffset]);
            }
            rightOffset++;
            if (leftOffset + rightOffset == n) {
                break;
            }
            // bottom
            for (int i = n - 1 - rightOffset; i >= leftOffset; i--) {
                result.add(matrix[m - 1 - bottomOffset][i]);
            }
            bottomOffset++;
            if (topOffset + bottomOffset == m) {
                break;
            }
            // left
            for (int i = m - 1 - bottomOffset; i >= topOffset; i--) {
                result.add(matrix[i][leftOffset]);
            }
            leftOffset++;
            if (leftOffset + rightOffset == n) {
                break;
            }
        }
        return result;
    }
}

Spiral Matrix 2

Example

Given
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
return 
[
  [ 1, 2, 3 ],
  [ 8, 9, 4 ],
  [ 7, 6, 5 ]
]

解法

public class Solution {
    public int[][] generateMatrix(int n) {
        int num = 1;
        int[][] res = new int[n][n];
        for (int cur = 0; cur < n/2; cur++) {
            for (int j = cur; j < n-1-cur; j++) {
                res[cur][j] = num++;
            }
            for (int i = cur; i < n-1-cur; i++) {
                res[i][n-1-cur] = num++;
            }
            for (int j = n-1-cur; j > cur; j--) {
                res[n-1-cur][j] = num++;
            }
            for (int i = n-1-cur; i > cur; i--) {
                res[i][cur] = num++;
            }
        }
        if (n % 2 != 0) res[n/2][n/2] = num;
        return res;
    }
}

相关文章

  • 54+59、Spiral Matrix、Spiral Matri

    Spiral Matrix Example 思路螺旋遍历输出一个m * n的数组。方向是上、右、下、左,依次循环。...

  • 54. Spiral Matrix

    54. Spiral Matrix 题目:https://leetcode.com/problems/spiral...

  • 对付所有的旋转矩阵

    **54、Spiral Matrix **Given a matrix of m x n elements (m ...

  • LeetCode 54 Spiral Matrix

    LeetCode 54 Spiral Matrix Given a matrix of m x n element...

  • 54. Spiral Matrix

    leetcode 54. Spiral Matrix Given a matrix of m x n eleme...

  • 每天(?)一道LeetCode(9) Spiral Matrix

    Array 054. Spiral Matrix Given a matrix of m x n elements...

  • Spiral Matrix

    好久没写文章了...自罚30大板在刷leetcode就继续更新吧 题目很好理解,就是把一个矩阵螺旋着输出。看到这个...

  • Spiral Matrix

    Easy, Msc Question 返回一个m x n的矩阵的螺旋序列 For example,矩阵:[[ 1,...

  • Spiral Matrix

    题意是将一个二维数组,螺旋遍历出来,那么从Matrix[0][0]开始,提前记录好上下左右四个方向的边界,并且按照...

  • Spiral Matrix

    在地铁上想出来的一道题 , 模拟出了四面"墙"的模型 伪代码:

网友评论

      本文标题:54+59、Spiral Matrix、Spiral Matri

      本文链接:https://www.haomeiwen.com/subject/qijbkftx.html