4ms Java Optimized code


  • 9
    S

    A head of a battleship means the top most or left most cell with value 'X'.
    Thus, we only need to count those heads.

    There are three rules to tell if a cell is a 'head':

    • The cell is a 'X' (board[i][j] == 'X')
    • No left side neighbor, or the left neighbor is a '.' (i == 0 || board[i - 1][j] == '.')
    • No right side neighbor, or the right neighbor is a '.' (j == 0 || board[i][j - 1] == '.')

    Code:

    public int countBattleships(char[][] board) {
    	if (board == null || board.length == 0 || board[0].length == 0) return 0;
    	int R = board.length, C = board[0].length, cnt = 0;
    	for (int i = 0; i < R; i++) {
    		for (int j = 0; j < C; j++) {
    			if (board[i][j] == 'X' && (i == 0 || board[i - 1][j] == '.') && (j == 0 || board[i][j - 1] == '.'))
    				cnt++;
    		}
    	}
    	
    	return cnt;
    }
    

    ...Note...
    For a statement like if (A && B && C), when A is false, the program will not compute B and C.
    So, for the best performance, we write the program in this way, instead of using a lot of if statements.


  • 0
    Z

    nice explanation,thanks!


  • 1
    Y

    you mean no left up side neighbor right?


Log in to reply
 

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