Skip to content

Commit 273e111

Browse files
committed
commit solution 36
1 parent fc8254d commit 273e111

File tree

6 files changed

+88
-6
lines changed

6 files changed

+88
-6
lines changed

Diff for: index-tags.md

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
5353
| --- | --- | --- | --- | --- |
5454
| [1](https://leetcode-cn.com/problems/two-sum) | [两数之和](/solution/1-99/0001.two-sum/) | `数组`,`哈希表` | <font color=green>简单</font> ||
55+
| [36](https://leetcode-cn.com/problems/valid-sudoku) | [有效的数独](/solution/1-99/0036.valid-sudoku/) | `哈希表` | <font color=blue>中等</font> ||
5556
| [136](https://leetcode-cn.com/problems/single-number) | [只出现一次的数字](/solution/100-199/0136.single-number/) | `位运算`,`哈希表` | <font color=green>简单</font> ||
5657
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> ||
5758
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> ||

Diff for: index-type.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@
423423
| [31](https://leetcode-cn.com/problems/next-permutation) | [下一个排列](/solution/1-99/0031.next-permutation/) | `数组` | <font color=blue>中等</font> |
424424
| [33](https://leetcode-cn.com/problems/search-in-rotated-sorted-array) | [搜索旋转排序数组](/solution/1-99/0033.search-in-rotated-sorted-array/) | `数组`,`二分查找` | <font color=blue>中等</font> |
425425
| [34](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array) | [在排序数组中查找元素的第一个和最后一个位置](/solution/1-99/0034.find-first-and-last-position-of-element-in-sorted-array/) | `数组`,`二分查找` | <font color=blue>中等</font> |
426-
| [36](https://leetcode-cn.com/problems/valid-sudoku) | [有效的数独](/solution/1-99/0036.valid-sudoku/) | `哈希表` | <font color=blue>中等</font> |
426+
| [36](https://leetcode-cn.com/problems/valid-sudoku) | [有效的数独](/solution/1-99/0036.valid-sudoku/) | `哈希表` | <font color=blue>中等</font> ||
427427
| [39](https://leetcode-cn.com/problems/combination-sum) | [组合总和](/solution/1-99/0039.combination-sum/) | `数组`,`回溯算法` | <font color=blue>中等</font> |
428428
| [40](https://leetcode-cn.com/problems/combination-sum-ii) | [组合总和 ii](/solution/1-99/0040.combination-sum-ii/) | `数组`,`回溯算法` | <font color=blue>中等</font> |
429429
| [43](https://leetcode-cn.com/problems/multiply-strings) | [字符串相乘](/solution/1-99/0043.multiply-strings/) | `数学`,`字符串` | <font color=blue>中等</font> |

Diff for: solution/1-99/0036.valid-sudoku/README.md

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# [36. 有效的数独](https://leetcode-cn.com/problems/valid-sudoku)
22

33
### 题目描述
4-
<!-- 这里写题目描述 -->
4+
55
<p>判断一个&nbsp;9x9 的数独是否有效。只需要<strong>根据以下规则</strong>,验证已经填入的数字是否有效即可。</p>
66

77
<ol>
@@ -67,12 +67,33 @@
6767

6868
### 具体解法
6969

70-
<!-- tabs:start -->
7170

7271
#### **Golang**
7372
```go
74-
73+
func isValidSudoku(board [][]byte) bool {
74+
// ascii 1-9 = 48-57
75+
var row, col, block [9][58]bool
76+
for k, nums := range board {
77+
for i, v := range nums {
78+
if v == '.' {
79+
continue
80+
}
81+
if row[i][v] {
82+
return false
83+
}
84+
if col[k][v] {
85+
return false
86+
}
87+
if block[i/3+k/3*3][v] {
88+
return false
89+
}
90+
row[i][v] = true
91+
col[k][v] = true
92+
block[i/3+k/3*3][v] = true
93+
}
94+
}
95+
return true
96+
}
7597
```
7698

77-
<!-- tabs:end -->
7899

Diff for: solution/1-99/0036.valid-sudoku/solution.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=36 lang=golang
5+
*
6+
* [36] 有效的数独
7+
*/
8+
9+
// @lc code=start
10+
func isValidSudoku(board [][]byte) bool {
11+
// ascii 1-9 = 48-57
12+
var row, col, block [9][58]bool
13+
for k, nums := range board {
14+
for i, v := range nums {
15+
if v == '.' {
16+
continue
17+
}
18+
if row[i][v] {
19+
return false
20+
}
21+
if col[k][v] {
22+
return false
23+
}
24+
if block[i/3+k/3*3][v] {
25+
return false
26+
}
27+
row[i][v] = true
28+
col[k][v] = true
29+
block[i/3+k/3*3][v] = true
30+
}
31+
}
32+
return true
33+
}
34+
35+
// @lc code=end

Diff for: solution/1-99/0036.valid-sudoku/solution_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestIsValidSudoku(t *testing.T) {
8+
var ret bool
9+
var nums [][]byte
10+
ret = true
11+
nums = [][]byte{
12+
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
13+
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
14+
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
15+
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
16+
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
17+
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
18+
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
19+
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
20+
{'.', '.', '.', '.', '8', '.', '.', '7', '9'},
21+
}
22+
if ret != isValidSudoku(nums) {
23+
t.Fatalf("case fails %v\n", ret)
24+
}
25+
}

Diff for: solution/1-99/_sidebar.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
- [33. 搜索旋转排序数组](solution/1-99/0033.search-in-rotated-sorted-array/)
4242
- [34. 在排序数组中查找元素的第一个和最后一个位置](solution/1-99/0034.find-first-and-last-position-of-element-in-sorted-array/)
4343
- [35. 搜索插入位置 ✅](solution/1-99/0035.search-insert-position/)
44-
- [36. 有效的数独](solution/1-99/0036.valid-sudoku/)
44+
- [36. 有效的数独](solution/1-99/0036.valid-sudoku/)
4545
- [37. 解数独](solution/1-99/0037.sudoku-solver/)
4646
- [38. 外观数列](solution/1-99/0038.count-and-say/)
4747
- [39. 组合总和](solution/1-99/0039.combination-sum/)

0 commit comments

Comments
 (0)