JAVA Solution, 19ms, beating more than 98%


  • 0
    G

    ''' public int findLonelyPixel(char[][] picture) {
    if(picture == null || picture.length == 0 || picture[0] == null || picture[0].length == 0) return 0;

        int exist[] = new int[picture[0].length];
        for(int i = 0; i< exist.length; i++){
            exist[i] = -1;
        }
        boolean row[] = new boolean[picture.length];
        
        for(int i = 0; i < picture.length; i++){
            int is = 0;
            for(int j = 0; j < picture[0].length; j++){
                if(picture[i][j] == 'B'){
                    is++;
                    if(exist[j] == -1) {
                       exist[j] = i;
                    }else{
                        exist[j] = -10;
                    }
                }        
            }
            if(is == 1) row[i] = true;
        }
        
        int sum = 0;
        for(int i = 0; i< exist.length; i++){
            if(exist[i] != -1 && exist[i] != -10){
                if(row[exist[i]]) sum++;
            }
        }
        
        return sum;
    }
    

    '''


Log in to reply
 

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