Commit 1a24a66d authored by yaozc's avatar yaozc
Browse files

36 solved

parent 19607155
......@@ -99,4 +99,40 @@ int searchInsert(vector<int>& nums, int target) {
}
}
return nums.size();
}
\ No newline at end of file
}
// 36 Valid Sudoku
// an easy median problem, used set to deal with the repeated num problem
// not fast, but works
bool isValidSudoku(vector<vector<char>>& board) {
vector<set<char> > rows;
vector<set<char> > cols;
vector<set<char> > boxs;
rows.resize(9);
cols.resize(9);
boxs.resize(9);
for(int r=0; r<9; r++){
for(int c=0; c<9; c++){
if(board[r][c] != '.'){
int box_index = (r/3) * 3 + c/3;
if( rows[r].find((board[r][c])) == rows[r].end() &&
cols[c].find((board[r][c])) == cols[c].end() &&
boxs[box_index].find(board[r][c]) == boxs[box_index].end()){
rows[r].insert(board[r][c]);
cols[c].insert(board[r][c]);
boxs[box_index].insert(board[r][c]);
}
else{
return false;
}
}
}
}
return true;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment