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

[59] Spiral Matrix II

对一个二维数组进行螺旋式填充
  • camlot_
  • camlot_
  • 2015年08月25日 17:38
  • 257

[LeetCode]--59. Spiral Matrix II

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

Leetcode NO.59 Spiral Matrix II

本题题目要求如下: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spi...

【LeetCode】59. Spiral Matrix II 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51416037Subject 出处:https://leetcode.com/pro...

LeetCode | 59. Spiral Matrix II

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

Leetcode 59. Spiral Matrix II

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

LeetCode54/59 Spiral Matrix I/II

一:Spiral Matrix I 题目: Given a matrix of m x n elements (m rows, n columns), return all elements ...

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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目29:59. Spiral Matrix II
举报原因:
原因补充:

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