Skip to content

Commit facd527

Browse files
committed
test: 36 solution
py, c++, go, java
1 parent a17f268 commit facd527

File tree

3 files changed

+102
-22
lines changed

3 files changed

+102
-22
lines changed

problems/problems_36/Solution.cpp

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,61 @@
11
//go:build ignore
22
#include "cpp/common/Solution.h"
33

4-
54
using namespace std;
65
using json = nlohmann::json;
76

7+
constexpr int N = 9;
8+
89
class Solution {
910
public:
10-
bool isValidSudoku(vector<vector<char>>& board) {
11-
11+
bool isValidSudoku(const vector<vector<char>> &board) {
12+
for (int i = 0; i < N; ++i) {
13+
int r = 0, c = 0, b = 0;
14+
for (int j = 0; j < N; ++j) {
15+
if (board[i][j] != '.') {
16+
int v = 1 << (board[i][j] - '1');
17+
if (r & v)
18+
return false;
19+
r |= v;
20+
}
21+
if (board[j][i] != '.') {
22+
int v = 1 << (board[j][i] - '1');
23+
if (c & v)
24+
return false;
25+
c |= v;
26+
}
27+
int bi = (i / 3) * 3 + j / 3;
28+
int bj = (i % 3) * 3 + j % 3;
29+
if (board[bi][bj] != '.') {
30+
int v = 1 << (board[bi][bj] - '1');
31+
if (b & v)
32+
return false;
33+
b |= v;
34+
}
35+
}
1236
}
37+
return true;
38+
}
1339
};
1440

1541
json leetcode::qubh::Solve(string input_json_values) {
16-
vector<string> inputArray;
17-
size_t pos = input_json_values.find('\n');
18-
while (pos != string::npos) {
19-
inputArray.push_back(input_json_values.substr(0, pos));
20-
input_json_values = input_json_values.substr(pos + 1);
21-
pos = input_json_values.find('\n');
22-
}
23-
inputArray.push_back(input_json_values);
42+
vector<string> inputArray;
43+
size_t pos = input_json_values.find('\n');
44+
while (pos != string::npos) {
45+
inputArray.push_back(input_json_values.substr(0, pos));
46+
input_json_values = input_json_values.substr(pos + 1);
47+
pos = input_json_values.find('\n');
48+
}
49+
inputArray.push_back(input_json_values);
2450

25-
Solution solution;
26-
vector<vector<string>> board_str = json::parse(inputArray.at(0));
27-
auto board = vector<vector<char>>(board_str.size(), vector<char>(board_str[0].size()));
28-
for (size_t i = 0; i < board.size(); ++i) {
29-
for (size_t j = 0; j < board[i].size(); ++j) {
30-
board[i][j] = board_str[i][j][0];
31-
}
32-
}
33-
return solution.isValidSudoku(board);
51+
Solution solution;
52+
vector<vector<string>> board_str = json::parse(inputArray.at(0));
53+
auto board =
54+
vector<vector<char>>(board_str.size(), vector<char>(board_str[0].size()));
55+
for (size_t i = 0; i < board.size(); ++i) {
56+
for (size_t j = 0; j < board[i].size(); ++j) {
57+
board[i][j] = board_str[i][j][0];
58+
}
59+
}
60+
return solution.isValidSudoku(board);
3461
}

problems/problems_36/Solution.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,31 @@
66

77

88
public class Solution extends BaseSolution {
9+
private static final int N = 9;
910
public boolean isValidSudoku(char[][] board) {
10-
11+
for (int i = 0; i < N; i++) {
12+
int r = 0, c = 0, b = 0;
13+
for (int j = 0; j < N; j++) {
14+
if (board[i][j] != '.') {
15+
int v = 1 << (board[i][j] - '1');
16+
if ((r & v) != 0) return false;
17+
r |= v;
18+
}
19+
if (board[j][i] != '.') {
20+
int v = 1 << (board[j][i] - '1');
21+
if ((c & v) != 0) return false;
22+
c |= v;
23+
}
24+
int bi = (i / 3) * 3 + j / 3;
25+
int bj = (i % 3) * 3 + j % 3;
26+
if (board[bi][bj] != '.') {
27+
int v = 1 << (board[bi][bj] - '1');
28+
if ((b & v) != 0) return false;
29+
b |= v;
30+
}
31+
}
32+
}
33+
return true;
1134
}
1235

1336
@Override

problems/problems_36/solution.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,38 @@ import (
66
"strings"
77
)
88

9+
const N = 9
10+
911
func isValidSudoku(board [][]byte) bool {
10-
12+
for i := range N {
13+
r, c, b := 0, 0, 0
14+
for j := range N {
15+
if board[i][j] != '.' {
16+
v := 1 << (board[i][j] - '1')
17+
if r&v != 0 {
18+
return false
19+
}
20+
r |= v
21+
}
22+
if board[j][i] != '.' {
23+
v := 1 << (board[j][i] - '1')
24+
if c&v != 0 {
25+
return false
26+
}
27+
c |= v
28+
}
29+
bi := (i/3)*3 + j/3
30+
bj := (i%3)*3 + j%3
31+
if board[bi][bj] != '.' {
32+
v := 1 << (board[bi][bj] - '1')
33+
if b&v != 0 {
34+
return false
35+
}
36+
b |= v
37+
}
38+
}
39+
}
40+
return true
1141
}
1242

1343
func Solve(inputJsonValues string) any {

0 commit comments

Comments
 (0)