one pass c++ naive solution


  • 0
    L
    bool isValidSudoku(vector<vector<char>>& board) {
    	for (int i=0; i<9; i++) {
    		bool a[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    		bool b[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    		bool c[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    
    		for (int j=0; j<9; j++) {
    			if (board[i][j] != '.') {
    				if (a[board[i][j]-'1']) return false;
    				a[board[i][j]-'1'] = true;
    			}
    			
    			if (board[j][i] != '.') {
    				if (b[board[j][i]-'1']) return false;
    				b[board[j][i]-'1'] = true;
    			}
    
    			int x = 3*(i/3%3)+(j/3%3);
    			int y = 3*(i%3)+(j%3);
    			if (board[x][y] != '.') {
    				if (c[board[x][y]-'1']) return false;
    				c[board[x][y]-'1'] = true;
    			}
    		}
    	}
    	return true;
    }
    

Log in to reply
 

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