Middle-题目29:59. Spiral Matrix II

原创 2016年05月31日 15:50:32

题目原文:
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
题目大意:
给一个整数n,输出n*n的方阵,其中1~n2以顺时针的螺旋方式填充。
题目分析:
就是简单的模拟,从1开始往里填,注意拐弯的临界条件(走到数组的边缘或者按原方向的下一位已经填好数)。
源码:(language:java)

public class Solution {
    private  final int RIGHT = 1;
    private  final int DOWN = 2;
    private  final int LEFT = 3;
    private  final int UP = 4;
    public  int[][] generateMatrix(int n) {
        int direction = 1;//1,2,3,4 stand for RIGHT,DOWN,LEFT,UP respectively.
        int row=0,col=0;//current position
        int[][] matrix = new int[n][n];
        for(int i=1;i<=n*n;i++) {
            matrix[row][col] = i;
            switch (direction) {
                case RIGHT: {                   
                    if(col+1 == n || matrix[row][col+1] != 0) {
                        direction=DOWN;
                        row++;
                    }
                    else
                        col++;
                    break;
                }
                case DOWN: {
                    if(row+1 == n || matrix[row+1][col] != 0) {
                        direction=LEFT;
                        col--;                      
                    }
                    else
                        row++;
                    break;
                }
                case LEFT: {
                    if(col == 0 || matrix[row][col-1] != 0) {
                        direction=UP;
                        row--;
                    }
                    else
                        col--;
                    break;
                }
                case UP: {
                    if(matrix[row-1][col] != 0) {
                        direction=RIGHT;
                        col++;
                    }
                    else
                        row--;
                }
            }
        }
        return matrix;
    }
}

成绩:
0ms,beats 14.13%,众数0ms,85.87%

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

Leetcode59. Spiral Matrix II

59. Spiral Matrix II 1、原题 Given an integer n, generate a square matrix filled with elements from 1...

leetcode 59. Spiral Matrix II-螺旋矩阵|清晰思路

原题链接:59. Spiral Matrix II 【思路】 遍历图解如上图所示,n 分为偶数和奇数两种情况。当 n 为偶数时,最后一次遍历刚好添加完毕;当 n 为奇数时,最后还需要进行一次添加操...

59. Spiral Matrix II

题目:Spiral Matrix II原题链接: https://leetcode.com/problems/spiral-matrix-ii/ Given an integer n, genera...

【LEETCODE】59-Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. Fo...

Spiral Matrix II - LeetCode 59

题目描述: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral orde...

228. Summary Ranges\59. Spiral Matrix II\64. Minimum Path Sum

Summary Ranges 题目描述 代码实现 Spiral Matrix II 题目描述 代码实现 Minimum Path Sum 题目描述 代码实现228. Summary Ranges题目描...

59. Spiral Matrix II

题意:给定一个整数n,生成一个把1到n^2按螺旋顺序填充的方阵。For example, Given n = 3, You should return the following matrix: ...

LeetCode#59. Spiral Matrix II

题目:给定一个整数n,以螺旋形将1到n^2填充螺旋矩阵

LeetCode 54/59. Spiral Matrix i, ii

1. 题目描述54 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...

leetcode 59. Spiral Matrix II

我在这里用了和leetcode 54 一样的思路开辟了一个和原矩阵同样大小的矩阵temp来存放是否输出过的标志位,然后按照向右,向左,向上,向下的顺时针顺序构造矩阵。 下面是这两道题的代码:...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)