【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】

【059-Spiral Matrix II(螺旋矩阵II)】


【LeetCode-面试算法经典-Java实现】【所有题目目录索引】

原题

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

题目大意

  给定一个整数n,生成一个n*n的矩阵,用1-n^2的数字进行螺旋填充。

解题思路

  采用计算生成法,对每一个位置计算对应的数。

代码实现

算法实现类

public class Solution {
    public int[][] generateMatrix(int n) {
        int[][] result = new int[n][n];

        int layer;
        int k;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                layer = layer(i, j, n); // 当前坐标外有几层
                // n * n - layer * layer外围层使用的最后一个数字(也是最大的)
                // 坐标所在的当前层使用的第一个数字
                k = n * n - (n - 2 * layer) * (n - 2 * layer) + 1;
                result[i][j] = k;

                // (n - 2 * layer - 1):四个(n - 2 * layer - 1)就是(x,y)坐标所在层的所有元素个数
                if (i == layer) { // 情况一、坐标离上边界最近
                    result[i][j] = k + j - layer;
                } else if (j == n - layer - 1) { // 情况二、坐标离右边界最近
                    result[i][j] = k + (n - 2 * layer - 1) + i - layer;
                } else if (i == n - layer - 1) { // 情况三、坐标离下边界最近
                    result[i][j] = k + 3 * (n - 2 * layer - 1) - (j - layer);
                } else { // 情况三、坐标离左边界最近
                    result[i][j] = k + 4 * (n - 2 * layer - 1) - (i - layer);
                }
            }
        }

        return result;
    }

    /**
     * 在一个n*n的矩阵中,计算(x,y)坐标外有多少层,坐标从0开始计算
     *
     * @param x 横坐标
     * @param y 纵坐标
     * @param n 矩阵大小
     * @return 坐标外的层数
     */
    public int layer(int x, int y, int n) {
        x = x < n - 1 - x ? x : n - 1 - x; // 计算横坐标离上下边界的最近距离
        y = y < n - 1 - y ? y : n - 1 - y; // 计算纵坐标离左右边界的最近距离

        return x < y ? x : y; // 较小的值为坐标的外围层数
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47164439

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-28 01:50:05

【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】的相关文章

[LeetCode] 885. Spiral Matrix III 螺旋矩阵之三

On a 2 dimensional grid with?R?rows and?C?columns, we start at?(r0, c0)?facing east. Here, the north-west corner of the grid is at the?first row and column, and the south-east corner of the grid is at the last row and column. Now, we walk in a clockw

leetCode 59.Spiral Matrix II (螺旋矩阵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 ] ] 思路:此题和螺旋矩阵题差不多,没什么难的地方,主要就是四个方向的转换. 具体代码如下: pu

LeetCode OJ: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]. 典型的dfs问题,与前面一个问题比较像,代码如下

【LeetCode-面试算法经典-Java实现】【054-Spiral Matrix(螺旋矩阵)】

[054-Spiral Matrix(螺旋矩阵)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 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 ]

【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", di

【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example

【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】

[064-Minimum Path Sum(最小路径和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either

【LeetCode-面试算法经典-Java实现】【056-Merge Intervals(区间合并)】

[056-Merge Intervals(区间合并)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 题目大意 给定一个区间集合,合并有重叠的区间. 解题思路 先对区间进行排序.按開始

【LeetCode-面试算法经典-Java实现】【066-Plus One(加一)】

[066-Plus One(加一)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 题目大意 给定一个用数组表示的一个数,