Java solution beat 78% easy understand


  • 0
    S

    just check is there duplicate number in horizontal or vertical or sub box

    int[] horizontal, vertical, cube;
    	for (int i = 0; i < 9; i++) {
    		horizontal = new int[9];
    		vertical = new int[9];
    		for (int j = 0; j < 9; j++) {
    			int h = board[i][j]-'1';
    			if (h>=0 && ++horizontal[h] >1) return false;
    			int v = board[j][i]-'1';
    			if (v>=0 && ++vertical[v] >1) return false;
    		}
    	}
    	for (int i = 1; i <=7; i=i+3) {
    		for (int j = 1; j <= 7; j=j+3) {
    			cube = new int[9];
    			for (int r = -1; r <=1; r++) {
    				for (int c = -1; c <= 1; c++) {
    					int v = board[j+r][i+c]-'1';
    				        if (v>=0 && ++cube[v] >1) return false;
    				}
    			}
    		}
    	}
    	return true;
    

Log in to reply
 

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