leetcode--普通题

题目1 . 输出螺旋矩阵

https://leetcode.com/problems/spiral-matrix/

题目描述:顺时针方向输出螺旋矩阵,逐步螺旋进中心,  将元素按此顺序输出

题解:  略

class Solution {
    boolean[][] A= null ;
    List<Integer> list = new ArrayList<>();
    public List<Integer> spiralOrder(int[][] matrix) {
        if(matrix.length == 0)return list;
        
        A = new boolean[matrix.length][matrix[0].length];
        
        //方向
        int[] x_o = new int[] {0,1,0,-1};
        int[] y_o = new int[] {1,0,-1,0};
        
        //先把0加上
        int x = 0;
        int y = 0;
        list.add(matrix[x][y]);
         A[x][y] = true;
        int count = 1;
        int number = matrix.length*matrix[0].length;
        
        while(count < number){
             for(int i=0;i<4;i++){
                while(canGo(x+x_o[i],y+y_o[i])){
                    x = x+x_o[i];
                    y = y+y_o[i];
                    list.add(matrix[x][y]);
                    A[x][y] = true;
                    count ++;
                }
            }
            
        }
       
        return list;
        
    }
    
    boolean canGo(int x,int y){
        int m = A.length-1;
        int n = A[0].length-1;
        if(x >=0 && x <=m && y>=0 && y<= n &&A[x][y] == false){
            return true;
        }
        return false;
    }
    
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值