Easy understand just one time scan with HashMap solution


  • 0
    T
    public int findLonelyPixel(char[][] picture) {
            int rowT = picture.length;
            if(rowT == 0){
                return 0;
            }
            int colT = picture[0].length;
            
            Map<Integer, Integer> map = new HashMap<>();
            
            for(int row=0; row < rowT; row++){
                List<Integer> rowBlack = new ArrayList<>();
                for(int col=0; col < colT; col++){
                    if(picture[row][col] == 'B'){
                        rowBlack.add(col);
                        if(map.containsKey(col)){
                            map.put(col, 0);
                        }else{
                            map.put(col, 1);
                        }
                    }
                }
                if(rowBlack.size()>1){
                    for(Integer rowCol: rowBlack){
                        map.put(rowCol, 0);
                    }
                }
            }
            int total = 0;
            for(Integer key : map.keySet()){
                if(map.get(key) > 0)
                    total ++;
            }
            return total;
        }
    

Log in to reply
 

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