Share my AC solution


  • 0
    S
    public char[][] updateBoard(char[][] board, int[] click) {
    	if (board.length == 0) return board;
    	int a = click[0], b = click[1];
    	if (board[a][b] == 'M') {
    		board[a][b] = 'X';
    	}else if (board[a][b] != 'B'){
    		Maround(board, a, b, board.length, board[0].length);
    	}
    	return board;
    }
    	
    private void Maround(char[][] board, int i, int j, int h, int w ){
    	board[i][j] = 'B';
    	int res = 0;
    	for (int m = -1; m <= 1; m++) {
    		for (int n = -1; n <= 1; n++) {
    			int x = i+m, y = j+n;
    			if (x>=0 && x<h && y>=0 && y<w){
    				if (board[x][y] == 'M') res++;
    			}
    		}
    	}
    	if (res > 0) board[i][j] = String.valueOf(res).charAt(0);
    	else {
    		for (int m = -1; m <= 1; m++) {
    			for (int n = -1; n <= 1; n++) {
    				int x = i+m, y = j+n;
    				if (x>=0 && x<h && y>=0 && y<w){
    					if (board[x][y] != 'B') {
    						Maround(board, x, y, h, w);
    					}
    				}
    			}
    		}
    	}
    }
    

Log in to reply
 

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