Skip to content

Commit 3fa8118

Browse files
author
zj972
committed
add 37.38.39.40. golang
1 parent fecffa7 commit 3fa8118

File tree

9 files changed

+573
-8
lines changed

9 files changed

+573
-8
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@
4040
|034|Find First and Last Position of Element in Sorted Array|[Golang](https://github.com/zj972/leetcode/tree/master/code/034.%20Find%20First%20and%20Last%20Position%20of%20Element%20in%20Sorted%20Array)|Medium|
4141
|035|Search Insert Position|[Golang](https://github.com/zj972/leetcode/tree/master/code/035.%20Search%20Insert%20Position)|Easy|
4242
|036|Valid Sudoku|[JavaScript & Golang](https://github.com/zj972/leetcode/tree/master/code/036.%20Valid%20Sudoku)|Easy|
43-
|038|Count and Say|[JavaScript](https://github.com/zj972/leetcode/tree/master/code/038.%20Count%20and%20Say)|Easy|
43+
|037|Sudoku Solver|[Golang](https://github.com/zj972/leetcode/tree/master/code/037.%20Sudoku%20Solver)|Hard|
44+
|038|Count and Say|[JavaScript & Golang](https://github.com/zj972/leetcode/tree/master/code/038.%20Count%20and%20Say)|Easy|
45+
|039|Combination Sum|[Golang](https://github.com/zj972/leetcode/tree/master/code/039.%20Combination%20Sum)|Medium|
46+
|040|Combination Sum II|[Golang](https://github.com/zj972/leetcode/tree/master/code/040.%20Combination%20Sum%20II)|Medium|
4447
|058|Length of Last Word|[JavaScript](https://github.com/zj972/leetcode/tree/master/code/058.%20Length%20of%20Last%20Word)|Easy|
4548
|062|Unique Paths|[JavaScript](https://github.com/zj972/leetcode/tree/master/code/062.%20Unique%20Paths)|Medium|
4649
|066|Plus One|[JavaScript](https://github.com/zj972/leetcode/tree/master/code/066.%20Plus%20One)|Easy|

code/037. Sudoku Solver/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 37. Sudoku Solver
2+
3+
Write a program to solve a Sudoku puzzle by filling the empty cells.
4+
5+
A sudoku solution must satisfy all of the following rules:
6+
7+
1.Each of the digits 1-9 must occur exactly once in each row.
8+
9+
2.Each of the digits 1-9 must occur exactly once in each column.
10+
11+
3.Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
12+
13+
Empty cells are indicated by the character '.'.
14+
15+
![img](https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
16+
17+
A sudoku puzzle...
18+
19+
![img](https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Sudoku-by-L2G-20050714_solution.svg/250px-Sudoku-by-L2G-20050714_solution.svg.png)
20+
21+
...and its solution numbers marked in red.
22+
23+
##### Note:
24+
25+
* The given board contain only digits 1-9 and the character '.'.
26+
27+
* You may assume that the given Sudoku puzzle will have a single unique solution.
28+
29+
* The given board size is always 9x9.

code/037. Sudoku Solver/main.go

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package main
2+
3+
import "fmt"
4+
5+
//Runtime: 172 ms, faster than 10.34% of Go online submissions for Sudoku Solver.
6+
//func solveSudoku(board [][]byte) {
7+
// var next func(board [][]byte, x int, y int) bool
8+
// next = func(board [][]byte, x int, y int) bool {
9+
// if board[x][y] != byte(46) {
10+
// if x == 8 && y == 8 {
11+
// return true
12+
// }
13+
// if x == 8 {
14+
// return next(board, 0, y+1)
15+
// } else {
16+
// return next(board, x+1, y)
17+
// }
18+
// }
19+
// list := verify(board, x, y)
20+
// if len(list) == 0 {
21+
// return false
22+
// }
23+
// if x == 8 && y == 8 {
24+
// if len(list) != 1 {
25+
// return false
26+
// }
27+
// board[x][y] = list[0]
28+
// return true
29+
// }
30+
// var v bool
31+
// for i := 0; i < len(list); i++ {
32+
// board[x][y] = list[i]
33+
// if x == 8 {
34+
// v = next(board, 0, y+1)
35+
// } else {
36+
// v = next(board, x+1, y)
37+
// }
38+
// if v {
39+
// return true
40+
// }
41+
// }
42+
// if v == false {
43+
// board[x][y] = byte(46)
44+
// }
45+
// return v
46+
// }
47+
// next(board, 0, 0)
48+
//}
49+
//
50+
//func verify(board [][]byte, x int, y int) []byte {
51+
// all := map[byte]bool{
52+
// 46: false,
53+
// 49: true,
54+
// 50: true,
55+
// 51: true,
56+
// 52: true,
57+
// 53: true,
58+
// 54: true,
59+
// 55: true,
60+
// 56: true,
61+
// 57: true,
62+
// }
63+
// for i := 0; i < len(board); i++ {
64+
// for j := 0; j < len(board[0]); j++ {
65+
// if i == x || j == y {
66+
// all[board[i][j]] = false
67+
// continue
68+
// }
69+
// xs := (x / 3) * 3
70+
// ys := (y / 3) * 3
71+
// if i >= xs && i <= xs+2 && j >= ys && j <= ys+2 {
72+
// all[board[i][j]] = false
73+
// continue
74+
// }
75+
// }
76+
// }
77+
// outPut := []byte{}
78+
// for k, v := range all {
79+
// if v {
80+
// outPut = append(outPut, k)
81+
// }
82+
// }
83+
// return outPut
84+
//}
85+
86+
//Runtime: 0 ms, faster than 100.00% of Go online submissions for Sudoku Solver.
87+
func solveSudoku(board [][]byte) {
88+
var colMark [9][9]bool
89+
var rowMark [9][9]bool
90+
var areaMark [9][9]bool
91+
92+
for i := 0; i < 9; i++ {
93+
for j := 0; j < 9; j++ {
94+
colMark[i][j] = true
95+
rowMark[i][j] = true
96+
areaMark[i][j] = true
97+
}
98+
}
99+
100+
for i := 0; i < 9; i++ {
101+
for j := 0; j < 9; j++ {
102+
if board[i][j] != '.' {
103+
num := int(board[i][j]-'0') - 1
104+
rowMark[i][num] = false
105+
colMark[j][num] = false
106+
index := (i/3)*3 + j/3
107+
areaMark[index][num] = false
108+
}
109+
}
110+
}
111+
112+
findSolved(board, 0, colMark, rowMark, areaMark)
113+
}
114+
115+
func findSolved(board [][]byte, count int, colMark, rowMark, areaMark [9][9]bool) bool {
116+
if count >= 81 {
117+
return true
118+
}
119+
row := count / 9
120+
col := count % 9
121+
area := (row/3)*3 + col/3
122+
if board[row][col] == '.' {
123+
for v := 0; v < 9; v++ {
124+
if colMark[col][v] && rowMark[row][v] && areaMark[area][v] {
125+
board[row][col] = byte('1' + v)
126+
colMark[col][v] = false
127+
rowMark[row][v] = false
128+
areaMark[area][v] = false
129+
if findSolved(board, count+1, colMark, rowMark, areaMark) {
130+
return true
131+
}
132+
board[row][col] = '.'
133+
colMark[col][v] = true
134+
rowMark[row][v] = true
135+
areaMark[area][v] = true
136+
}
137+
}
138+
} else {
139+
return findSolved(board, count+1, colMark, rowMark, areaMark)
140+
}
141+
142+
return false
143+
}
144+
145+
func main() {
146+
var data = [][]byte{
147+
[]byte("53..7...."),
148+
[]byte("6..195..."),
149+
[]byte(".98....6."),
150+
[]byte("8...6...3"),
151+
[]byte("4..8.3..1"),
152+
[]byte("7...2...6"),
153+
[]byte(".6....28."),
154+
[]byte("...419..5"),
155+
[]byte("....8..79"),
156+
}
157+
for i := 0; i < len(data); i++ {
158+
fmt.Printf("%v:", i)
159+
for j := 0; j < len(data[i]); j++ {
160+
fmt.Printf(" %v", string(data[i][j]))
161+
if j == 2 || j == 5 {
162+
fmt.Printf(" |")
163+
}
164+
}
165+
fmt.Printf("\n")
166+
if i == 2 || i == 5 {
167+
fmt.Printf("— — — — — — — — — — — ———\n")
168+
}
169+
}
170+
fmt.Printf("--------------------------------------\n")
171+
solveSudoku(data)
172+
for i := 0; i < len(data); i++ {
173+
fmt.Printf("%v:", i)
174+
for j := 0; j < len(data[i]); j++ {
175+
fmt.Printf(" %v", string(data[i][j]))
176+
if j == 2 || j == 5 {
177+
fmt.Printf(" |")
178+
}
179+
}
180+
fmt.Printf("\n")
181+
if i == 2 || i == 5 {
182+
fmt.Printf("— — — — — — — — — — — ———\n")
183+
}
184+
}
185+
}

code/038. Count and Say/README.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
# 38. Count and Say
2-
The count-and-say sequence is the sequence of integers beginning as follows:
32

4-
`1, 11, 21, 1211, 111221, ...`
3+
The count-and-say sequence is the sequence of integers with the first five terms as following:
54

6-
`1` is read off as `"one 1"` or `11`.
5+
1. 1
6+
2. 11
7+
3. 21
8+
4. 1211
9+
5. 111221
710

8-
`11` is read off as `"two 1s"` or `21`.
11+
1 is read off as "one 1" or 11.
912

10-
`21` is read off as `"one 2`, then `one 1"` or `1211`.
13+
11 is read off as "two 1s" or 21.
1114

12-
Given an integer n, generate the `n<sup>th</sup>` sequence.
15+
21 is read off as "one 2, then one 1" or 1211.
1316

14-
Note: The sequence of integers will be represented as a string.
17+
Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence.
18+
19+
Note: Each term of the sequence of integers will be represented as a string.
20+
21+
##### Example 1:
22+
23+
Input: 1
24+
25+
Output: "1"
26+
27+
##### Example 2:
28+
29+
Input: 4
30+
31+
Output: "1211"

code/038. Count and Say/main.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
)
7+
8+
//Runtime: 4 ms, faster than 58.25% of Go online submissions for Count and Say.
9+
//func countAndSay(n int) string {
10+
// var str = "1"
11+
// var getStr = func(str string) (next string) {
12+
// for i := 0; i < len(str); i++ {
13+
// if i == len(str)-1 {
14+
// next += "1" + string(str[i])
15+
// } else {
16+
// var n int = 0
17+
// var v = str[i]
18+
// for i < len(str) {
19+
// if str[i] == v {
20+
// n++
21+
// i++
22+
// } else {
23+
// next += strconv.Itoa(n) + string(v)
24+
// n = 0
25+
// i--
26+
// break
27+
// }
28+
// }
29+
// if n != 0 {
30+
// next += strconv.Itoa(n) + string(v)
31+
// }
32+
// }
33+
// }
34+
// return next
35+
// }
36+
// for i := 1; i < n; i++ {
37+
// str = getStr(str)
38+
// }
39+
// return str
40+
//}
41+
42+
//Runtime: 0 ms, faster than 100.00% of Go online submissions for Count and Say.
43+
func countAndSay(n int) string {
44+
if n == 1 {
45+
return "1"
46+
}
47+
lastString := "1"
48+
for i := 2; i <= n; i++ {
49+
var results []byte
50+
lastByte := lastString[0]
51+
num := 1
52+
for j := range lastString {
53+
if j == 0 {
54+
continue
55+
}
56+
if lastString[j] == lastByte {
57+
num++
58+
} else {
59+
results = append(results, byte(num+48))
60+
results = append(results, lastByte)
61+
lastByte = lastString[j]
62+
num = 1
63+
}
64+
}
65+
results = append(results, byte(num+48))
66+
results = append(results, lastByte)
67+
lastString = string(results)
68+
}
69+
return lastString
70+
}
71+
72+
func main() {
73+
fmt.Println(countAndSay(6))
74+
}

0 commit comments

Comments
 (0)