leetcode 之Spiral Matrix I 和 II 解题思路

来源:互联网 发布:淘宝刷客究竟犯不犯法 编辑:程序博客网 时间:2024/06/04 18:51

题目如下:

Spiral Matrix

 My Submissions

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

题意:就是按照螺旋形状打印出二维数组。

螺旋形状需要有四个方向,右,下,左,上,再依次进行。当在向右移动时,遇到边界或者后面一个已经打印过,则改变方向,向下;其它方向依次相同。因此需要有一个二维数组记录当前的位置是否打印过,还需要有一个变量记录已经打印了多少个,当打印的个数和二维数组总个数相同时,则停止打印。

代码如下:

public class Solution {    public List<Integer> spiralOrder(int[][] matrix) {        List<Integer> list = new ArrayList<Integer>();        int nRows = matrix.length;        if(nRows == 0) return list;        int nCols = matrix[0].length;        int total_Len = nRows * nCols;//数组总长度        int[][] used = new int[nRows][nCols];//用于判断matrix数组中的元素是否打印过                int current_DIR = 0;//当前方向        int i = 0; int j = 0;        int len = 0;        while(true){        if(len == total_Len){//如果当前打印长度和总长度相等,则停止打印        break;        }        switch(current_DIR){        case 0:{//0代表向右的方向        if(j == nCols || used[i][j] == 1){//改变方向的边界条件        j--;        i++;        current_DIR = 1;        }else{        list.add(matrix[i][j]);        used[i][j] = 1;        len++;        j++;        }        break;        }        case 1:{//1代表向下        if(i == nRows || used[i][j] == 1){        i--;        j--;        current_DIR = 2;        }else{        list.add(matrix[i][j]);        used[i][j] = 1;        len++;        i++;        }        break;        }        case 2:{//2代表向左        if(j == -1 || used[i][j] == 1){        j++;        i--;        current_DIR = 3;        }else{        list.add(matrix[i][j]);        used[i][j] = 1;        len++;        j--;        }        break;        }        case 3:{//3代表向上        if(i == -1 || used[i][j] == 1){        i++;        j++;        current_DIR = 0;        }else{        list.add(matrix[i][j]);        used[i][j] = 1;        len++;        i--;        }        }        }        }        return list;    }}


Spiral Matrix II

 Total Accepted: 10563 Total Submissions: 34583My Submissions

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的矩阵,方法和打印类似,直接给出代码

public class Solution {    public int[][] generateMatrix(int n) {         if(n == 0) return new int[0][0];        int[][] matrix = new int[n][n];        int len = 0;        int total_Len = n * n;        int current_Dir = 0;        int[][] used = new int[n][n];        int i = 0, j = 0;        while(true){        if(len == total_Len){        break;        }        switch(current_Dir){        case 0:{        if(j == n || used[i][j] == 1){        j--;        i++;        current_Dir = 1;        }else{        matrix[i][j] = ++len;        used[i][j] = 1;        j++;        }        break;        }        case 1:{        if(i == n || used[i][j] == 1){        i--;        j--;        current_Dir = 2;        }else{        matrix[i][j] = ++len;        used[i][j] = 1;        i++;        }        break;        }        case 2:{        if(j == -1 || used[i][j] == 1){        j++;        i--;        current_Dir = 3;        }else{        matrix[i][j] = ++len;        used[i][j] = 1;        j--;        }        break;        }        case 3:{        if(i == -1 || used[i][j] == 1){        i++;        j++;        current_Dir = 0;        }else{        matrix[i][j] = ++len;        used[i][j] = 1;        i--;        }        break;        }        }                }        return matrix;    }}



0 0