leetcodeのSpiral Matrix IとIIの解題の構想


タイトルは以下の通りです.

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] .
2 D配列を螺旋状にプリントアウトします.
螺旋の形は4つの方向が必要で、右、下、左、上、さらに順次行います.右に移動すると、境界に遭遇したり、後ろの1つが印刷されたりすると、方向を変えて下に下がります.他の方向は順番に同じです.そのため、現在の位置が印刷されたかどうかを記録する2次元配列と、印刷された個数と2次元配列の合計個数が同じである場合、印刷を停止する変数記録が必要です.
コードは次のとおりです.
public class Solution {
    public List spiralOrder(int[][] matrix) {
        List list = new ArrayList();
        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: 34583 My 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に基づいて螺旋の形状に従って1つの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;
    }
}