Concise efficient java code


  • 0
    K

    >! >! ``` public static boolean valid(char[][] board){

    	Set<Character> row = new HashSet<>();
    	Set<Character> col = new HashSet<>();
    	Set<Character> cube = new HashSet<>();
    	for (int i = 0; i < 9; i++) {
    		for (int j = 0; j < 9; j++) {
    
                  if (board[i][j] != '.' && !row.add(board[i][j])) { //check if the set already has the value
    				return false;
    			}
                   if (board[j][i] != '.' && !col.add(board[j][i])) {
    				return false;
    			}
    int rowindex = 3 * (i / 3);
    int colindex = 3 * (i % 3);
    if (board[rowindex + j / 3][colindex + j % 3] != '.'
    	&& !cube.add(board[rowindex + j / 3][colindex + j % 3])) {
    				return false;
    			}
    		}
    		row.clear();
    		col.clear();
    		cube.clear();
    	}
    	return true;
    }
    ```

Log in to reply
 

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