Why is my code not working? It has the same idea with the AC solutions here


  • 0
    J
    I really have no idea why I can't get this right. Thank you in advance!
        public class Solution {
        public void wallsAndGates(int[][] rooms) {
            if (rooms==null||rooms.length==0) return;
            Queue<int[]> queue=new LinkedList<>();
            for(int i=0;i<rooms.length;i++){
                for(int j=0;j<rooms[0].length;j++){
                    if(rooms[i][j]==0){
                        queue.add(new int[]{i,j});
                    }
                }
            }
            int[][] diff={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
            while(!queue.isEmpty()){
                int[] current=queue.remove();
                for(int[] d:diff){
                    if(current[0]+d[0]>=0&¤t[0]+d[0]<rooms.length&¤t[1]+d[1]>=0&¤t[1]+d[1]<rooms[0].length&&rooms[current[0]+d[0]][current[1]+d[1]]>rooms[current[0]][current[1]]+1){
                        rooms[current[0]+d[0]][current[1]+d[1]]=rooms[current[0]][current[1]]+1;
                        queue.add(new int[]{current[0]+d[0],current[1]+d[1]});
                    }
                }
            }
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.