Java tricky solution


  • 1
    S
    if (board == null) return 0;
    	double count = 0;
    	int height = board.length;
    	int width = board[0].length;
    	for (int i = 0; i < height; i++) {
    		for (int j = 0; j < width; j++) {
    			if (board[i][j] == '.') continue;
    			int near = 0;
    			
    			if (i!=0 && board[i-1][j] == 'X') near++;
    			if (i!=height-1 && board[i+1][j] == 'X') near++;
    			
    			if (j!=0 && board[i][j-1] == 'X') near++;
    			if (j!=width-1 && board[i][j+1] == 'X') near++;
    			
    			if (near == 1) count += 0.5;
    			else if (near == 0) count += 1;
    		}
    	}
    	return (int)count;
    

Log in to reply
 

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