Wrong Compile process


  • 0
    P

    class Point{
    int x;
    int y;
    public Point(int x, int y){
    this.x = x;
    this.y = y;
    }
    }

    public class Solution {

    static int idOfIslands = 0;
    static Point moveType[];
    static boolean visited[][];
    
    public boolean isCorrectRange(Point point, char grid[][]){
        if((point.x >= 0 && point.x <= grid.length - 1) && (point.y >= 0 && point.y <= grid[0].length-1)){
            return true;
        }
        else{
            return false;
        }
    }
    
    public int numIslands(char[][] grid) {
        
        int xLength = grid.length;
        if(grid.length == 0 ){
            return 0;
        }
        int yLength = grid[0].length;
        visited = new boolean[xLength][yLength];
        moveType = new Point[4];
        Queue <Point>queue = new LinkedList<Point>();
        for(int x = 0 ; x < grid.length ; x++){
            
            for(int y = 0 ; y < grid[0].length ; y++){
                
                if(grid[x][y] == '1' && visited[x][y] == false){
                    queue.offer(new Point(x,y));
                    visited[x][y] = true;
                    
                    while(!queue.isEmpty()){
                        
                        Point tempPoint = queue.poll();
                        //Top
                        moveType[0] = new Point(tempPoint.x - 1, tempPoint.y);
                        //Bottom
                        moveType[1] = new Point(tempPoint.x + 1, tempPoint.y);
                        //Left
                        moveType[2] = new Point(tempPoint.x, tempPoint.y -1);
                        //Right
                        moveType[3] = new Point(tempPoint.x , tempPoint.y + 1);
                        
                        for(int i = 0 ; i < moveType.length ; i++){
                            if(isCorrectRange(moveType[i],grid) && !visited[moveType[i].x][moveType[i].y] && grid[moveType[i].x][moveType[i].y] == '1'){
                                queue.offer(moveType[i]);
                                visited[moveType[i].x][moveType[i].y] = true;
                            }
                        }
    
                    }
                    idOfIslands++;
                }
            }
        }
        
        return idOfIslands;
        
        
    }
    

    }

    this testcase,
    ["11000","11000","00100","00011"]

    if i click Run Code button, I pass this problem(answer is 3)
    but, i submit this solution, i cant pass.( my answer is 4.. strange.. :( )
    please help me!


Log in to reply
 

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