Index: [Leetcode] leetcode key index (C++/JAVA/PYTHON/SQL)
Github:https://github.com/illuz/leetcode
036. Valid Sudoku (Easy)
links:
Title: https://leetcode.com/problems/valid-sudoku/
Code (GitHub): Https://github.com/illuz/leetcode
Test Instructions:
Determine if a Sudoku is valid.
Effective Sudoku is not forced to have a solution.
Analysis:
As long as there is no same number in the same row, column, block.
Open the number of records on the line, there is no difficulty, you can use the binary to represent, the table bit operation acceleration.
(Note that the judgment is valid, not a solution, I just started to solve, TLE a lot of times ...) )
Code:
C++:
class Solution {Private:int row[9], col[9], sqr[3][3];bool Check (int x, int y, int val) {return! (row[x]>>val) &1) &&! ((col[y]>>val) &1) &&! ((Sqr[x/3][y/3]>>val) &1);} void mark (int x, int y, int val) {row[x] |= (1<<val); Col[y] |= (1<<val); SQR[X/3][Y/3] |= (1<<val);} <pre name= "code" class= "Java" >//solve Sudoku
void unmark (int x, int y, int val) {//row[x]-= (1<<val);//col[y]-= (1<<val);//SQR[X/3][Y/3]-= (1<<v AL);//}//bool dfs (int pos, vector<vector<char> > &board) {////x = pos/9, y = pos% 9//if (pos = = Bayi)//r Eturn true;//if (board[pos/9][pos%9]! = '. ') {//return DFS (pos + 1, board);/} else {//for (int i = 0; i < 9; i++)//if (check (POS/9, pos%9, i)) {//mark (POS/9), pos%9 , i);//if (DFS (POS + 1, board))//return True;//unmark (POS/9, pos%9, i);//}//}//return False;//}public:bool Isvalidsudo KU (vector<vector<char> > &board) {memset (row, 0, sizeof), memset (col, 0, sizeof (COL)), memset (SQR, 0, sizeof (SQR)); for (int i = 0; i < board.size (); i++) for (int j = 0; J < Board[i].size (), j + +) if (board[i][j]! =. ') {if (!check (i, J, Board[i][j]-' 1 ')) return False;mark (i, J, Board[i][j]-' 1 ');} Return true;//return DFS (0, board); }};
[Leetcode] 036. Valid Sudoku (Easy) (C + +)