Skip to content

Commit 647a2af

Browse files
committed
Add another two C++ solutions for leetcode 36-数独.
1 parent 82c82d5 commit 647a2af

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <vector>
2+
#include <iostream>
3+
// #include <algorithm>
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
bool isValidSudoku(vector<vector<char>>& board) {
9+
vector<int> row(9); //row[j]表示第j 行的9个数字的存在情况,同理与col, boxes
10+
vector<int> col(9);
11+
vector<int> boxes(9);
12+
13+
int shiftCount = 0;
14+
for (int i = 0; i < 9; i++)
15+
{
16+
for (int j = 0; j < 9; j++)
17+
{
18+
if (board[i][j] == '.')
19+
continue;
20+
21+
shiftCount = 1 << (board[i][j] - '0'); // 转为二进制,目标位为1,其他位均为0
22+
// 每个格子若有数字,必为 1 ~ 9,转换成对应的整型数字,1 → 0001,2 → 0010,3→0100,4→1000 ...
23+
int boxPos = (i / 3) * 3 + j / 3; //将大数独棋盘分成9个小棋盘,编号0~8
24+
25+
// 如果当前数字shiftCount在row[j] 或col[i] 或 boxes中已经存在,&运算后不为0,
26+
// 只有当前数字没出现过,&运算后为0
27+
if ((col[i] & shiftCount) != 0 || (row[j] & shiftCount) != 0 || (boxes[boxPos] & shiftCount) != 0)
28+
return false;
29+
30+
//第 n 位代表 n 这个数字是否存在(1→存在, 0→不存在),同理于col[i] boxes[boxPos]
31+
row[j] |= shiftCount;
32+
col[i] |= shiftCount;
33+
boxes[boxPos] |= shiftCount;
34+
}
35+
}
36+
return true;
37+
}
38+
};
39+
40+
// Test
41+
int main()
42+
{
43+
Solution sol;
44+
vector<vector<char>> board /* 不能使用=进行赋值初始化, 自动调用构造函数 */
45+
{
46+
{'5','3','.','.','7','.','.','.','.'},
47+
{'6','.','.','1','9','5','.','.','.'},
48+
{'.','9','8','.','.','.','.','6','.'},
49+
{'8','.','.','.','6','.','.','.','3'},
50+
{'4','.','.','8','.','3','.','.','1'},
51+
{'7','.','.','.','2','.','.','.','6'},
52+
{'.','6','.','.','.','.','2','8','.'},
53+
{'.','.','.','4','1','9','.','.','5'},
54+
{'.','.','.','.','8','.','.','7','9'}
55+
};
56+
57+
bool isValid = sol.isValidSudoku(board);
58+
cout << (isValid == true ? "true" : "false") << endl;
59+
60+
return 0;
61+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <algorithm>
2+
#include <string>
3+
#include <vector>
4+
#include <set> /* 使用set就够了,不一定需要unordered_set */
5+
#include <iostream>
6+
using namespace std;
7+
8+
class Solution {
9+
public:
10+
bool isValidSudoku(vector<vector<char>>& board) {
11+
set<string> st;
12+
for (int i = 0; i < 9; i++) {
13+
for (int j = 0; j < 9; j++) {
14+
char ch = board[i][j];
15+
// 使用i / 3 + "," + j / 3 得到对应第几行第几列的方块(box)
16+
if (ch != '.'){
17+
string val;
18+
val.push_back(ch);
19+
/* 对于任意一个值不为'.'的字符
20+
1.把所在row的信息插入到大九宫格中;
21+
2.把所在column的信息插入到大九宫格中;
22+
3.把所在的小方块(box)的信息插入到大九宫格中。
23+
插入如果失败说明出现了重复。 */
24+
if (!st.insert(val + " in row " + to_string(i)).second ||
25+
!st.insert(val + " in column " + to_string(j)).second ||
26+
!st.insert(val + " in box " + to_string(i / 3) + "," + to_string(j / 3)).second)
27+
return false; /* set插入失败时,表示出现了重复 */
28+
}
29+
}
30+
}
31+
return true;
32+
}
33+
};
34+
35+
// Test
36+
int main()
37+
{
38+
Solution sol;
39+
vector<vector<char>> board /* 不能使用=进行赋值初始化, 自动调用构造函数 */
40+
{
41+
{'5','3','.','.','7','.','.','.','.'},
42+
{'6','.','.','1','9','5','.','.','.'},
43+
{'.','9','8','.','.','.','.','6','.'},
44+
{'8','.','.','.','6','.','.','.','3'},
45+
{'4','.','.','8','.','3','.','.','1'},
46+
{'7','.','.','.','2','.','.','.','6'},
47+
{'.','6','.','.','.','.','2','8','.'},
48+
{'.','.','.','4','1','9','.','.','5'},
49+
{'.','.','.','.','8','.','.','7','9'}
50+
};
51+
52+
bool isValid = sol.isValidSudoku(board);
53+
cout << (isValid == true ? "true" : "false") << endl;
54+
55+
return 0;
56+
}

0 commit comments

Comments
 (0)