Valid Sudoku Solutions in C++
Number 36
Difficulty Medium
Acceptance 48.8%
Link LeetCode
Other languages Go
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/valid-sudoku/// Author : Hao Chen// Date : 2014-07-17class Solution {public:bool isValidSudoku(vector<vector<char> > &board) {const int cnt = 9;bool row_mask[cnt][cnt] = {false};bool col_mask[cnt][cnt] = {false};bool area_mask[cnt][cnt] = {false};//check each rows and colsfor(int r=0; r<board.size(); r++){for (int c=0; c<board[r].size(); c++){if (!isdigit(board[r][c])) continue;int idx = board[r][c] - '0' - 1;//check the rowsif (row_mask[r][idx] == true){return false;}row_mask[r][idx] = true;//check the colsif (col_mask[c][idx] == true) {return false;}col_mask[c][idx] = true;//check the areasint area = (r/3) * 3 + (c/3);if (area_mask[area][idx] == true) {return false;}area_mask[area][idx] = true;}}return true;}};
C++ solution by pezy/LeetCode
#include <vector>#include <unordered_map>#include <unordered_set>using std::vector;class Solution {public:bool isValidSudoku(vector<vector<char> > &board) {std::unordered_map<size_t, std::unordered_set<char>> row_map, col_map, cel_map;for (size_t i=0; i<board.size(); ++i)for (size_t j=0; j<board[0].size(); ++j) {if (board[i][j] == '.') continue;if (!row_map[i].insert(board[i][j]).second) return false;if (!col_map[j].insert(board[i][j]).second) return false;if (!cel_map[i/3 * 10 + j/3].insert(board[i][j]).second) return false;}return true;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/valid-sudoku//// Author : liuyubobobo/// Time : 2016-12-05#include <iostream>#include <vector>#include <cassert>#include <stdexcept>using namespace std;/// Using hashtable to check every row, column and block./// Time Complexity: O(9*9)/// Space Complexity: O(1)class Solution {public:bool isValidSudoku(vector<vector<char>>& board) {bool hashtable[9][9];// check for every rowmemset(hashtable, 0, sizeof(hashtable));for(int i = 0 ; i < 9 ; i ++)for(int j = 0 ; j < 9 ; j ++)if(board[i][j] != '.'){int num = board[i][j] - '0' - 1;if(hashtable[i][num] == 1)return false;hashtable[i][num] = 1;}// check for every colmemset(hashtable, 0, sizeof(hashtable));for(int i = 0 ; i < 9 ; i ++)for(int j = 0 ; j < 9 ; j ++)if(board[j][i] != '.'){int num = board[j][i] - '0' - 1;if( hashtable[i][num] == 1 )return false;hashtable[i][num] = 1;}// check for every blockmemset(hashtable, 0, sizeof(hashtable));for(int i = 0 ; i < 9 ; i ++){int br = i/3;int bc = i%3;for(int ii = 0 ; ii < 3 ; ii ++)for(int jj = 0 ; jj < 3 ; jj ++){int r = br*3 + ii;int c = bc*3 + jj;if(board[r][c] != '.'){int num = board[r][c] - '0' - 1;if (hashtable[i][num] == 1)return false;hashtable[i][num] = 1;}}}return true;}};int main() {vector<string> input = {"53..7....","6..195...",".98....6.","8...6...3","4..8.3..1","7...2...6",".6....28.","...419..5","....8..79"};vector<vector<char>> board;for(const string& row: input){vector<char> r;for(char c: row)r.push_back(c);board.push_back(r);}cout << Solution().isValidSudoku(board) << endl;return 0;}