Clean Java O(m*n) with explanation!


  • 0

    //Scan every line of picture, if a pixel 'B' in this line are lonely, then check if it is lonely from the column pespective. If so, count this pixel, otherwise, move to the next line.

    public class Solution {
    public int findLonelyPixel(char[][] picture) {

    	int count = 0;
    	
    	for (int i=0;i<picture.length;i++){
    		String line = new String(picture[i]);
    		int first = line.indexOf('B');
    		int last = line.lastIndexOf('B');
    		if (first < 0 ) continue;
    		else {
    			if(first == last && checkColumn(picture, i, first)) count++;
    		}
    	}
    	
    	return count;
    }
    
    private boolean checkColumn(char[][] picture, int x, int y){
    	for (int i=0;i<picture.length;i++){
    		if(picture[i][y]=='B'&&i!=x) return false;
    	}
    	return true;
    }
    

    }


Log in to reply
 

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