simple c++ solution, just 25 lines


  • 0
    L
    class Solution {
    public:
        bool isValidSudoku(vector<vector<char>>& board) {
        	short	colcheck[9] = { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 };
        	short	subcheck[3][3] = { {0x0000, 0x0000, 0x0000}, {0x0000, 0x0000, 0x0000}, {0x0000, 0x0000, 0x0000} };
        	for ( int i(0); i<board.size() && i<9; ++i){
        		short rowcheck(0x0000);
        		vector<char> &row = board[i];
        		for ( int j(0); j<row.size() && j<9; ++j ){
        			if ('.'==row[j]) continue;
        				
        			int	val(row[j]-'1'), r(i/3), c(j/3);
        
        			if ( (rowcheck & (0x01<<val)) || (colcheck[j] & (0x01<<val)) || (subcheck[r][c] & (0x01<<val)) )
        				return false;
        
        			rowcheck		|= 0x01<<val;
        			colcheck[j]		|= 0x01<<val;
        			subcheck[r][c]	|= 0x01<<val;
        		}
        	}
        
        	return true;
        }
    };
    

Log in to reply
 

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