Skip to content

Commit 0a36063

Browse files
committed
test: [20250831] Add (37)
1 parent 899ca7a commit 0a36063

File tree

12 files changed

+202
-29
lines changed

12 files changed

+202
-29
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ members = [
303303
"problems/problems_3446",
304304
"problems/problems_3021",
305305
"problems/problems_36",
306+
"problems/problems_37",
306307
]
307308

308309
[package]
@@ -628,3 +629,4 @@ solution_3459 = { path = "problems/problems_3459", features = ["solution_3459"]
628629
solution_3446 = { path = "problems/problems_3446", features = ["solution_3446"] }
629630
solution_3021 = { path = "problems/problems_3021", features = ["solution_3021"] }
630631
solution_36 = { path = "problems/problems_36", features = ["solution_36"] }
632+
solution_37 = { path = "problems/problems_37", features = ["solution_37"] }

daily-problems.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"daily": "1051",
2+
"daily": "37",
33
"plans": ["3652", "problems", "3653", "problems", "3654", "problems"]
44
}

golang/solution_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package golang
22

33
import (
4-
problem "leetCode/problems/problems_1051"
4+
problem "leetCode/problems/problems_37"
55
"testing"
66
)
77

88
func TestSolution(t *testing.T) {
9-
TestEach(t, "1051", "problems", problem.Solve)
9+
TestEach(t, "37", "problems", problem.Solve)
1010
}

problems/problems_37/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "solution_37"
3+
version = "0.1.0"
4+
edition = "2021"
5+
rust-version = "1.79.0"
6+
authors = ["benhao"]
7+
description = "LeetCode Solution 37 in Rust"
8+
readme = "../../README.md"
9+
10+
[features]
11+
solution_37 = []
12+
13+
[dependencies]
14+
serde_json = "1.0"
15+
rand = "0.8.4"
16+
regex = "1.10.5"
17+
library = { path = "../../rust/library", features = ["model"] }
18+
19+
[lib]
20+
name = "solution_37"
21+
path = "solution.rs"

problems/problems_37/Solution.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//go:build ignore
2+
#include "cpp/common/Solution.h"
3+
4+
5+
using namespace std;
6+
using json = nlohmann::json;
7+
8+
class Solution {
9+
public:
10+
void solveSudoku(vector<vector<char>>& board) {
11+
12+
}
13+
};
14+
15+
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);
24+
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+
solution.solveSudoku(board);
34+
return board;
35+
}

problems/problems_37/problem.md

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
1-
# 37. Sudoku Solver
1+
# 37. Sudoku Solver
22

3-
Write a program to solve a Sudoku puzzle by filling the empty cells.
3+
<p>Write a program to solve a Sudoku puzzle by filling the empty cells.</p>
44

5-
A sudoku solution must satisfy **all of the following rules**:
5+
<p>A sudoku solution must satisfy <strong>all of the following rules</strong>:</p>
66

7-
1. Each of the digits `1-9` must occur exactly once in each row.
8-
2. Each of the digits `1-9` must occur exactly once in each column.
9-
3. Each of the digits `1-9` must occur exactly once in each of the 9 `3x3` sub-boxes of the grid.
7+
<ol>
8+
<li>Each of the digits <code>1-9</code> must occur exactly once in each row.</li>
9+
<li>Each of the digits <code>1-9</code> must occur exactly once in each column.</li>
10+
<li>Each of the digits <code>1-9</code> must occur exactly once in each of the 9 <code>3x3</code> sub-boxes of the grid.</li>
11+
</ol>
1012

11-
The `'.'` character indicates empty cells.
13+
<p>The <code>&#39;.&#39;</code> character indicates empty cells.</p>
1214

13-
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png" style="height:250px; width:250px" />
18+
<pre>
19+
<strong>Input:</strong> board = [[&quot;5&quot;,&quot;3&quot;,&quot;.&quot;,&quot;.&quot;,&quot;7&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;6&quot;,&quot;.&quot;,&quot;.&quot;,&quot;1&quot;,&quot;9&quot;,&quot;5&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;9&quot;,&quot;8&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;6&quot;,&quot;.&quot;],[&quot;8&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;6&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;3&quot;],[&quot;4&quot;,&quot;.&quot;,&quot;.&quot;,&quot;8&quot;,&quot;.&quot;,&quot;3&quot;,&quot;.&quot;,&quot;.&quot;,&quot;1&quot;],[&quot;7&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;2&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;6&quot;],[&quot;.&quot;,&quot;6&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;2&quot;,&quot;8&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;4&quot;,&quot;1&quot;,&quot;9&quot;,&quot;.&quot;,&quot;.&quot;,&quot;5&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;8&quot;,&quot;.&quot;,&quot;.&quot;,&quot;7&quot;,&quot;9&quot;]]
20+
<strong>Output:</strong> [[&quot;5&quot;,&quot;3&quot;,&quot;4&quot;,&quot;6&quot;,&quot;7&quot;,&quot;8&quot;,&quot;9&quot;,&quot;1&quot;,&quot;2&quot;],[&quot;6&quot;,&quot;7&quot;,&quot;2&quot;,&quot;1&quot;,&quot;9&quot;,&quot;5&quot;,&quot;3&quot;,&quot;4&quot;,&quot;8&quot;],[&quot;1&quot;,&quot;9&quot;,&quot;8&quot;,&quot;3&quot;,&quot;4&quot;,&quot;2&quot;,&quot;5&quot;,&quot;6&quot;,&quot;7&quot;],[&quot;8&quot;,&quot;5&quot;,&quot;9&quot;,&quot;7&quot;,&quot;6&quot;,&quot;1&quot;,&quot;4&quot;,&quot;2&quot;,&quot;3&quot;],[&quot;4&quot;,&quot;2&quot;,&quot;6&quot;,&quot;8&quot;,&quot;5&quot;,&quot;3&quot;,&quot;7&quot;,&quot;9&quot;,&quot;1&quot;],[&quot;7&quot;,&quot;1&quot;,&quot;3&quot;,&quot;9&quot;,&quot;2&quot;,&quot;4&quot;,&quot;8&quot;,&quot;5&quot;,&quot;6&quot;],[&quot;9&quot;,&quot;6&quot;,&quot;1&quot;,&quot;5&quot;,&quot;3&quot;,&quot;7&quot;,&quot;2&quot;,&quot;8&quot;,&quot;4&quot;],[&quot;2&quot;,&quot;8&quot;,&quot;7&quot;,&quot;4&quot;,&quot;1&quot;,&quot;9&quot;,&quot;6&quot;,&quot;3&quot;,&quot;5&quot;],[&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;2&quot;,&quot;8&quot;,&quot;6&quot;,&quot;1&quot;,&quot;7&quot;,&quot;9&quot;]]
21+
<strong>Explanation:</strong>&nbsp;The input board is shown above and the only valid solution is shown below:
1422

15-
**Example 1:**
23+
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Sudoku-by-L2G-20050714_solution.svg/250px-Sudoku-by-L2G-20050714_solution.svg.png" style="height:250px; width:250px" />
24+
</pre>
1625

17-
![img](https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
26+
<p>&nbsp;</p>
27+
<p><strong>Constraints:</strong></p>
1828

19-
```
20-
Input: board = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
21-
Output: [["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","9"]]
22-
Explanation: The input board is shown above and the only valid solution is shown below:
23-
```
24-
25-
26-
27-
**Constraints:**
28-
29-
- `board.length == 9`
30-
- `board[i].length == 9`
31-
- `board[i][j]` is a digit or `'.'`.
32-
- It is **guaranteed** that the input board has only one solution.
29+
<ul>
30+
<li><code>board.length == 9</code></li>
31+
<li><code>board[i].length == 9</code></li>
32+
<li><code>board[i][j]</code> is a digit or <code>&#39;.&#39;</code>.</li>
33+
<li>It is <strong>guaranteed</strong> that the input board has only one solution.</li>
34+
</ul>

problems/problems_37/problem_zh.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 37. 解数独
2+
3+
<p>编写一个程序,通过填充空格来解决数独问题。</p>
4+
5+
<p>数独的解法需<strong> 遵循如下规则</strong>:</p>
6+
7+
<ol>
8+
<li>数字&nbsp;<code>1-9</code>&nbsp;在每一行只能出现一次。</li>
9+
<li>数字&nbsp;<code>1-9</code>&nbsp;在每一列只能出现一次。</li>
10+
<li>数字&nbsp;<code>1-9</code>&nbsp;在每一个以粗实线分隔的&nbsp;<code>3x3</code>&nbsp;宫内只能出现一次。(请参考示例图)</li>
11+
</ol>
12+
13+
<p>数独部分空格内已填入了数字,空白格用&nbsp;<code>'.'</code>&nbsp;表示。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<div class="top-view__1vxA">
18+
<div class="original__bRMd">
19+
<div>
20+
<p><strong>示例 1:</strong></p>
21+
<img src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/04/12/250px-sudoku-by-l2g-20050714svg.png" style="height:250px; width:250px" />
22+
<pre>
23+
<strong>输入:</strong>board = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
24+
<strong>输出:</strong>[["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","9"]]
25+
<strong>解释:</strong>输入的数独如上图所示,唯一有效的解决方案如下所示:
26+
27+
<img src=" https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/04/12/250px-sudoku-by-l2g-20050714_solutionsvg.png" style="height:250px; width:250px" />
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
32+
<p><strong>提示:</strong></p>
33+
34+
<ul>
35+
<li><code>board.length == 9</code></li>
36+
<li><code>board[i].length == 9</code></li>
37+
<li><code>board[i][j]</code> 是一位数字或者 <code>'.'</code></li>
38+
<li>题目数据 <strong>保证</strong> 输入数独仅有一个解</li>
39+
</ul>
40+
</div>
41+
</div>
42+
</div>

problems/problems_37/solution.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package problem37
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"strings"
7+
)
8+
9+
func solveSudoku(board [][]byte) {
10+
11+
}
12+
13+
func Solve(inputJsonValues string) any {
14+
inputValues := strings.Split(inputJsonValues, "\n")
15+
var board [][]byte
16+
17+
var boardStr [][]string
18+
if err := json.Unmarshal([]byte(inputValues[0]), &boardStr); err != nil {
19+
log.Fatal(err)
20+
}
21+
board = make([][]byte, len(boardStr))
22+
for i := 0; i < len(board); i++ {
23+
board[i] = make([]byte, len(boardStr[i]))
24+
for j := 0; j < len(board[i]); j++ {
25+
board[i][j] = boardStr[i][j][0]
26+
}
27+
}
28+
29+
solveSudoku(board)
30+
return byteArrToStrArr(board)
31+
}
32+
33+
func byteArrToStrArr(arr [][]byte) []string {
34+
ans := make([]string, len(arr))
35+
for i, b := range arr {
36+
ans[i] = string(b)
37+
}
38+
return ans
39+
}

problems/problems_37/solution.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use serde_json::{json, Value};
2+
3+
pub struct Solution;
4+
5+
impl Solution {
6+
pub fn solve_sudoku(board: &mut Vec<Vec<char>>) {
7+
8+
}
9+
}
10+
11+
#[cfg(feature = "solution_37")]
12+
pub fn solve(input_string: String) -> Value {
13+
let input_values: Vec<String> = input_string.split('\n').map(|x| x.to_string()).collect();
14+
let mut board: Vec<Vec<char>> = serde_json::from_str(&input_values[0]).expect("Failed to parse input");
15+
Solution::solve_sudoku(&mut board);
16+
json!(board)
17+
}

problems/problems_37/solution.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
Do not return anything, modify board in-place instead.
3+
*/
4+
function solveSudoku(board: string[][]): void {
5+
6+
};
7+
8+
export function Solve(inputJsonElement: string): any {
9+
const inputValues: string[] = inputJsonElement.split("\n");
10+
const board: string[][] = JSON.parse(inputValues[0]);
11+
solveSudoku(board)
12+
return board;
13+
}

0 commit comments

Comments
 (0)