Skip to content

Commit 4321b6b

Browse files
committed
feat: finish 34 36
1 parent 8b7a778 commit 4321b6b

File tree

6 files changed

+261
-0
lines changed

6 files changed

+261
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# [34. Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/)
2+
3+
## 2019/09/14
4+
5+
### 题目 💗[medium]
6+
7+
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
8+
9+
Your algorithm's runtime complexity must be in the order of O(log n).
10+
11+
If the target is not found in the array, return [-1, -1].
12+
13+
Example 1:
14+
15+
```bash
16+
Input: nums = [5,7,7,8,8,10], target = 8
17+
Output: [3,4]
18+
```
19+
20+
Example 2:
21+
22+
```bash
23+
Input: nums = [5,7,7,8,8,10], target = 6
24+
Output: [-1,-1]
25+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package searchRange
2+
3+
func searchRange(nums []int, target int) []int {
4+
firstIndex := indexOf(nums, target)
5+
lastIndex := lastIndexOf(nums, target)
6+
return []int{firstIndex, lastIndex}
7+
}
8+
9+
func indexOf(nums []int, val int) int {
10+
for i := range nums {
11+
if nums[i] == val {
12+
return i
13+
}
14+
}
15+
return -1
16+
}
17+
18+
func lastIndexOf(nums []int, val int) int {
19+
for i := len(nums) - 1; i >= 0; i-- {
20+
if nums[i] == val {
21+
return i
22+
}
23+
}
24+
return -1
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package searchRange
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
var tcs = []struct {
10+
N1 []int
11+
N2 int
12+
ans []int
13+
}{
14+
{
15+
[]int{5, 7, 7, 8, 8, 10},
16+
8,
17+
[]int{3, 4},
18+
},
19+
}
20+
21+
func Test_bitwiseComplement(t *testing.T) {
22+
ast := assert.New(t)
23+
for _, tc := range tcs {
24+
ast.Equal(tc.ans, searchRange(tc.N1, tc.N2), "输入:%v", tc)
25+
}
26+
}

algorithm/36.ValidSudoku/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# [36. Valid Sudoku](https://leetcode.com/problems/valid-sudoku/)
2+
3+
## 2019/09/14
4+
5+
### 题目 💗[medium]
6+
7+
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
8+
9+
1. Each row must contain the digits 1-9 without repetition.
10+
2. Each column must contain the digits 1-9 without repetition.
11+
3. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.
12+
13+
A partially filled sudoku which is valid.
14+
15+
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
16+
17+
Example 1:
18+
19+
```bash
20+
Input:
21+
[
22+
["5","3",".",".","7",".",".",".","."],
23+
["6",".",".","1","9","5",".",".","."],
24+
[".","9","8",".",".",".",".","6","."],
25+
["8",".",".",".","6",".",".",".","3"],
26+
["4",".",".","8",".","3",".",".","1"],
27+
["7",".",".",".","2",".",".",".","6"],
28+
[".","6",".",".",".",".","2","8","."],
29+
[".",".",".","4","1","9",".",".","5"],
30+
[".",".",".",".","8",".",".","7","9"]
31+
]
32+
Output: true
33+
```
34+
35+
Example 2:
36+
37+
```bash
38+
Input:
39+
[
40+
["8","3",".",".","7",".",".",".","."],
41+
["6",".",".","1","9","5",".",".","."],
42+
[".","9","8",".",".",".",".","6","."],
43+
["8",".",".",".","6",".",".",".","3"],
44+
["4",".",".","8",".","3",".",".","1"],
45+
["7",".",".",".","2",".",".",".","6"],
46+
[".","6",".",".",".",".","2","8","."],
47+
[".",".",".","4","1","9",".",".","5"],
48+
[".",".",".",".","8",".",".","7","9"]
49+
]
50+
Output: false
51+
Explanation: Same as Example 1, except with the 5 in the top left corner being
52+
modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
53+
```
54+
55+
Note:
56+
57+
- A Sudoku board (partially filled) could be valid but is not necessarily solvable.
58+
- Only the filled cells need to be validated according to the mentioned rules.
59+
- The given board contain only digits 1-9 and the character '.'.
60+
- The given board size is always 9x9.

algorithm/36.ValidSudoku/main.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package isValidSudoku
2+
3+
import "fmt"
4+
5+
func isValidSudoku(board [][]byte) bool {
6+
// ◀️️️️️️ ️ ️ ️ ️ ️ ️ ️ ️ ️ ️ ◀️️️️️️ ️ ️ ️ ️ ️ ️ ️ ️ ️ ️ ◀️️️️️️ ️ ️ ️ ️ ️ ️ ️ ️ ️ ️ ◀️️️️️️ ️ ️ ️ ️ ️ ️ ️ ️ ️ ️ ️
7+
// check line
8+
for _, col := range board {
9+
Map := map[byte]int{}
10+
for _, e := range col {
11+
if e != '.' {
12+
if _, ok := Map[e]; ok {
13+
return false
14+
} else {
15+
Map[e] = 1
16+
}
17+
}
18+
}
19+
}
20+
// check column
21+
// 🔽🔽🔽🔽🔽🔽🔽🔽🔽🔽🔽🔽
22+
for i, col := range board {
23+
Map := map[byte]int{}
24+
for j := range col {
25+
e := board[j][i]
26+
if e != '.' {
27+
if _, ok := Map[e]; ok {
28+
return false
29+
} else {
30+
Map[e] = 1
31+
}
32+
}
33+
}
34+
}
35+
// check 3x3 box
36+
for i := 0; i < 3; i++ {
37+
for j := 0; j < 3; j++ {
38+
if !isSame3X3(board, i, j) {
39+
return false
40+
}
41+
}
42+
}
43+
return true
44+
}
45+
46+
func isSame3X3(board [][]byte, m, n int) bool {
47+
fmt.Println("===================")
48+
Map := map[byte]int{}
49+
for i := 3 * m; i < 3*(m+1); i++ {
50+
for j := 3 * n; j < 3*(n+1); j++ {
51+
e := board[i][j]
52+
if e != '.' {
53+
// fmt.Println(e, i, j)
54+
if _, ok := Map[e]; ok {
55+
return false
56+
} else {
57+
Map[e] = 1
58+
}
59+
}
60+
}
61+
}
62+
return true
63+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package isValidSudoku
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
var tcs = []struct {
10+
N1 [][]byte
11+
ans bool
12+
}{
13+
{
14+
[][]byte{
15+
[]byte{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
16+
[]byte{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
17+
[]byte{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
18+
[]byte{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
19+
[]byte{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
20+
[]byte{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
21+
[]byte{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
22+
[]byte{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
23+
[]byte{'.', '.', '.', '.', '8', '.', '.', '7', '9'},
24+
},
25+
true,
26+
},
27+
{
28+
[][]byte{
29+
[]byte{'8', '3', '.', '.', '7', '.', '.', '.', '.'},
30+
[]byte{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
31+
[]byte{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
32+
[]byte{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
33+
[]byte{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
34+
[]byte{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
35+
[]byte{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
36+
[]byte{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
37+
[]byte{'.', '.', '.', '.', '8', '.', '.', '7', '9'},
38+
},
39+
false,
40+
},
41+
{
42+
[][]byte{
43+
[]byte{'.', '.', '.', '.', '5', '.', '.', '1', '.'},
44+
[]byte{'.', '4', '.', '3', '.', '.', '.', '.', '.'},
45+
[]byte{'.', '.', '.', '.', '.', '3', '.', '.', '1'},
46+
[]byte{'8', '.', '.', '.', '.', '.', '.', '2', '.'},
47+
[]byte{'.', '.', '2', '.', '7', '.', '.', '.', '.'},
48+
[]byte{'.', '1', '5', '.', '.', '.', '.', '.', '.'},
49+
[]byte{'.', '.', '.', '.', '.', '2', '.', '.', '.'},
50+
[]byte{'.', '2', '.', '9', '.', '.', '.', '.', '.'},
51+
[]byte{'.', '.', '4', '.', '.', '.', '.', '.', '.'},
52+
},
53+
false,
54+
},
55+
}
56+
57+
func Test_bitwiseComplement(t *testing.T) {
58+
ast := assert.New(t)
59+
for _, tc := range tcs {
60+
ast.Equal(tc.ans, isValidSudoku(tc.N1), "输入:%v", tc)
61+
}
62+
}

0 commit comments

Comments
 (0)