Fast Java Implementation by Array rathe than Set


  • 5
    R
    public boolean isValidSudoku(char[][] board) {
        int size = board.length;
        
        for(int i = 0; i < size; i++)
        {
            boolean b1[] = new boolean[size];
            for(int j = 0; j < size; j++)
            {
                if(board[i][j] != '.')
                {
                    int diff = board[i][j] - '1';
                    if(b1[diff])
                        return false;
                    b1[diff] = true;
                }
            }
            
            boolean b2[] = new boolean[size];
            for(int k = 0; k < size; k++)
            {
                if(board[k][i] != '.')
                {
                    int diff = board[k][i] - '1';
                    if(b2[diff])
                        return false;
                    b2[diff] = true;
                }
            }
            
            boolean b3[] = new boolean[size];
            for(int m = 0; m < size; m++)
            {
                int x = m / 3 + (i / 3) * 3;
                int y = m % 3 + (i % 3) * 3;
                if(board[x][y] != '.')
                {
                    int diff = board[x][y] - '1';
                    if(b3[diff])
                        return false;
                    b3[diff] = true;
                }
            }
        }
        return true;
    }

  • 0
    T

    Can you please add some comments to your code to get a general sense of your approach? would be much better to understand.


  • 0
    A

    Very clever solution for inner boxes:


  • 0
    R

    Your code is clean and awesome. I add some comments to it so it would be more understandable.

     public boolean isValidSudokuWithArrays(char[][] board) {
        		int size = board.length;
        		
        		for (int i = 0; i < size; i++) {
        			// Check rows
        			boolean[] row = new boolean[size];
        			for (int j = 0; j < size; j++) {
        				if (board[i][j] != '.') {
        					// The number in board[i][j] is 1-9, convert it to 0-8 so that it can be filled into the array
        					int diff = board[i][j] - '1'; 
        					if (row[diff])
        						return false;
        					row[diff] = true;
        				}
        			}
        			
        			// Check columns
        			boolean[] col = new boolean[size];
        			for (int k = 0; k < size; k++) {
        				if (board[k][i] != '.') {
        					int diff = board[k][i] - '1';
        					if (col[diff])
        						return false;
        					col[diff] = true;
        				}
        			}
        			
        			// Check the inner boxes
        			boolean[] box = new boolean[size];
        			for (int m = 0; m < size; m++) {
        				int x = m / 3 + (i / 3) * 3;
        				int y = m % 3 + (i % 3) * 3;
        				if (board[x][y] != '.') {
        					int diff = board[x][y] - '1';
        					if (box[diff])
        						return false;
        					box[diff] = true;
        				}
        			}
        		}
        		return true;
        	}

  • 0
    R

    Thank you very much


  • 0
    R

    Sorry for the late reply. U can take a look of the comments below.


Log in to reply
 

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