Skip to content

Commit 1369d22

Browse files
authored
feat: use any instead of interface{} in golang (#1937)
1 parent c52c7c9 commit 1369d22

File tree

341 files changed

+4638
-4638
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

341 files changed

+4638
-4638
lines changed

lcci/04.12.Paths with Sum/Solution.go

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
/**
2-
* Definition for a binary tree node.
3-
* type TreeNode struct {
4-
* Val int
5-
* Left *TreeNode
6-
* Right *TreeNode
7-
* }
8-
*/
9-
func pathSum(root *TreeNode, sum int) int {
10-
cnt := map[int]int{0: 1}
11-
var dfs func(*TreeNode, int) int
12-
dfs = func(root *TreeNode, s int) int {
13-
if root == nil {
14-
return 0
15-
}
16-
s += root.Val
17-
ans := cnt[s-sum]
18-
cnt[s]++
19-
ans += dfs(root.Left, s)
20-
ans += dfs(root.Right, s)
21-
cnt[s]--
22-
return ans
23-
}
24-
return dfs(root, 0)
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func pathSum(root *TreeNode, sum int) int {
10+
cnt := map[int]int{0: 1}
11+
var dfs func(*TreeNode, int) int
12+
dfs = func(root *TreeNode, s int) int {
13+
if root == nil {
14+
return 0
15+
}
16+
s += root.Val
17+
ans := cnt[s-sum]
18+
cnt[s]++
19+
ans += dfs(root.Left, s)
20+
ans += dfs(root.Right, s)
21+
cnt[s]--
22+
return ans
23+
}
24+
return dfs(root, 0)
2525
}
+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
func insertBits(N int, M int, i int, j int) int {
2-
for k := i; k <= j; k++ {
3-
N &= ^(1 << k)
4-
}
5-
return N | M<<i
1+
func insertBits(N int, M int, i int, j int) int {
2+
for k := i; k <= j; k++ {
3+
N &= ^(1 << k)
4+
}
5+
return N | M<<i
66
}

lcci/05.04.Closed Number/Solution.go

+29-29
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
func findClosedNumbers(num int) []int {
2-
ans := []int{-1, -1}
3-
dirs := [3]int{0, 1, 0}
4-
for p := 0; p < 2; p++ {
5-
a, b := dirs[p], dirs[p+1]
6-
x := num
7-
for i := 1; i < 31; i++ {
8-
if x>>i&1 == a && x>>(i-1)&1 == b {
9-
x ^= 1 << i
10-
x ^= 1 << (i - 1)
11-
j, k := 0, i-2
12-
for j < k {
13-
for j < k && x>>j&1 == b {
14-
j++
15-
}
16-
for j < k && x>>k&1 == a {
17-
k--
18-
}
19-
if j < k {
20-
x ^= 1 << j
21-
x ^= 1 << k
22-
}
23-
}
24-
ans[p] = x
25-
break
26-
}
27-
}
28-
}
29-
return ans
1+
func findClosedNumbers(num int) []int {
2+
ans := []int{-1, -1}
3+
dirs := [3]int{0, 1, 0}
4+
for p := 0; p < 2; p++ {
5+
a, b := dirs[p], dirs[p+1]
6+
x := num
7+
for i := 1; i < 31; i++ {
8+
if x>>i&1 == a && x>>(i-1)&1 == b {
9+
x ^= 1 << i
10+
x ^= 1 << (i - 1)
11+
j, k := 0, i-2
12+
for j < k {
13+
for j < k && x>>j&1 == b {
14+
j++
15+
}
16+
for j < k && x>>k&1 == a {
17+
k--
18+
}
19+
if j < k {
20+
x ^= 1 << j
21+
x ^= 1 << k
22+
}
23+
}
24+
ans[p] = x
25+
break
26+
}
27+
}
28+
}
29+
return ans
3030
}
+40-40
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
const mod = 1e9 + 7
2-
3-
func waysToStep(n int) (ans int) {
4-
if n < 4 {
5-
return int(math.Pow(2, float64(n-1)))
6-
}
7-
a := [][]int{{1, 1, 0}, {1, 0, 1}, {1, 0, 0}}
8-
res := pow(a, n-4)
9-
for _, x := range res[0] {
10-
ans = (ans + x) % mod
11-
}
12-
return
13-
}
14-
15-
func mul(a, b [][]int) [][]int {
16-
m, n := len(a), len(b[0])
17-
c := make([][]int, m)
18-
for i := range c {
19-
c[i] = make([]int, n)
20-
}
21-
for i := 0; i < m; i++ {
22-
for j := 0; j < n; j++ {
23-
for k := 0; k < len(b); k++ {
24-
c[i][j] = (c[i][j] + a[i][k]*b[k][j]%mod) % mod
25-
}
26-
}
27-
}
28-
return c
29-
}
30-
31-
func pow(a [][]int, n int) [][]int {
32-
res := [][]int{{4, 2, 1}}
33-
for n > 0 {
34-
if n&1 == 1 {
35-
res = mul(res, a)
36-
}
37-
a = mul(a, a)
38-
n >>= 1
39-
}
40-
return res
1+
const mod = 1e9 + 7
2+
3+
func waysToStep(n int) (ans int) {
4+
if n < 4 {
5+
return int(math.Pow(2, float64(n-1)))
6+
}
7+
a := [][]int{{1, 1, 0}, {1, 0, 1}, {1, 0, 0}}
8+
res := pow(a, n-4)
9+
for _, x := range res[0] {
10+
ans = (ans + x) % mod
11+
}
12+
return
13+
}
14+
15+
func mul(a, b [][]int) [][]int {
16+
m, n := len(a), len(b[0])
17+
c := make([][]int, m)
18+
for i := range c {
19+
c[i] = make([]int, n)
20+
}
21+
for i := 0; i < m; i++ {
22+
for j := 0; j < n; j++ {
23+
for k := 0; k < len(b); k++ {
24+
c[i][j] = (c[i][j] + a[i][k]*b[k][j]%mod) % mod
25+
}
26+
}
27+
}
28+
return c
29+
}
30+
31+
func pow(a [][]int, n int) [][]int {
32+
res := [][]int{{4, 2, 1}}
33+
for n > 0 {
34+
if n&1 == 1 {
35+
res = mul(res, a)
36+
}
37+
a = mul(a, a)
38+
n >>= 1
39+
}
40+
return res
4141
}
+8-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
func multiply(A int, B int) int {
2-
if B == 1 {
3-
return A
4-
}
5-
if B&1 == 1 {
6-
return (multiply(A, B>>1) << 1) + A
7-
}
8-
return multiply(A, B>>1) << 1
1+
func multiply(A int, B int) int {
2+
if B == 1 {
3+
return A
4+
}
5+
if B&1 == 1 {
6+
return (multiply(A, B>>1) << 1) + A
7+
}
8+
return multiply(A, B>>1) << 1
99
}

lcci/08.06.Hanota/Solution.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
func hanota(A []int, B []int, C []int) []int {
2-
stk := []Task{{len(A), &A, &B, &C}}
3-
for len(stk) > 0 {
4-
task := stk[len(stk)-1]
5-
stk = stk[:len(stk)-1]
6-
if task.n == 1 {
7-
*task.c = append(*task.c, (*task.a)[len(*task.a)-1])
8-
*task.a = (*task.a)[:len(*task.a)-1]
9-
} else {
10-
stk = append(stk, Task{task.n - 1, task.b, task.a, task.c})
11-
stk = append(stk, Task{1, task.a, task.b, task.c})
12-
stk = append(stk, Task{task.n - 1, task.a, task.c, task.b})
13-
}
14-
}
15-
return C
16-
}
17-
18-
type Task struct {
19-
n int
20-
a, b, c *[]int
1+
func hanota(A []int, B []int, C []int) []int {
2+
stk := []Task{{len(A), &A, &B, &C}}
3+
for len(stk) > 0 {
4+
task := stk[len(stk)-1]
5+
stk = stk[:len(stk)-1]
6+
if task.n == 1 {
7+
*task.c = append(*task.c, (*task.a)[len(*task.a)-1])
8+
*task.a = (*task.a)[:len(*task.a)-1]
9+
} else {
10+
stk = append(stk, Task{task.n - 1, task.b, task.a, task.c})
11+
stk = append(stk, Task{1, task.a, task.b, task.c})
12+
stk = append(stk, Task{task.n - 1, task.a, task.c, task.b})
13+
}
14+
}
15+
return C
16+
}
17+
18+
type Task struct {
19+
n int
20+
a, b, c *[]int
2121
}

lcci/08.08.Permutation II/Solution.go

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
func permutation(S string) (ans []string) {
2-
cs := []byte(S)
3-
sort.Slice(cs, func(i, j int) bool { return cs[i] < cs[j] })
4-
t := []byte{}
5-
n := len(cs)
6-
vis := make([]bool, n)
7-
var dfs func(int)
8-
dfs = func(i int) {
9-
if i == n {
10-
ans = append(ans, string(t))
11-
return
12-
}
13-
for j := 0; j < n; j++ {
14-
if vis[j] || (j > 0 && !vis[j-1] && cs[j] == cs[j-1]) {
15-
continue
16-
}
17-
vis[j] = true
18-
t = append(t, cs[j])
19-
dfs(i + 1)
20-
t = t[:len(t)-1]
21-
vis[j] = false
22-
}
23-
}
24-
dfs(0)
25-
return
1+
func permutation(S string) (ans []string) {
2+
cs := []byte(S)
3+
sort.Slice(cs, func(i, j int) bool { return cs[i] < cs[j] })
4+
t := []byte{}
5+
n := len(cs)
6+
vis := make([]bool, n)
7+
var dfs func(int)
8+
dfs = func(i int) {
9+
if i == n {
10+
ans = append(ans, string(t))
11+
return
12+
}
13+
for j := 0; j < n; j++ {
14+
if vis[j] || (j > 0 && !vis[j-1] && cs[j] == cs[j-1]) {
15+
continue
16+
}
17+
vis[j] = true
18+
t = append(t, cs[j])
19+
dfs(i + 1)
20+
t = t[:len(t)-1]
21+
vis[j] = false
22+
}
23+
}
24+
dfs(0)
25+
return
2626
}

lcci/08.13.Pile Box/Solution.go

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
func pileBox(box [][]int) (ans int) {
2-
sort.Slice(box, func(i, j int) bool {
3-
a, b := box[i], box[j]
4-
return a[0] < b[0] || (a[0] == b[0] && b[1] < a[1])
5-
})
6-
n := len(box)
7-
f := make([]int, n)
8-
for i := 0; i < n; i++ {
9-
for j := 0; j < i; j++ {
10-
if box[j][1] < box[i][1] && box[j][2] < box[i][2] {
11-
f[i] = max(f[i], f[j])
12-
}
13-
}
14-
f[i] += box[i][2]
15-
ans = max(ans, f[i])
16-
}
17-
return
1+
func pileBox(box [][]int) (ans int) {
2+
sort.Slice(box, func(i, j int) bool {
3+
a, b := box[i], box[j]
4+
return a[0] < b[0] || (a[0] == b[0] && b[1] < a[1])
5+
})
6+
n := len(box)
7+
f := make([]int, n)
8+
for i := 0; i < n; i++ {
9+
for j := 0; j < i; j++ {
10+
if box[j][1] < box[i][1] && box[j][2] < box[i][2] {
11+
f[i] = max(f[i], f[j])
12+
}
13+
}
14+
f[i] += box[i][2]
15+
ans = max(ans, f[i])
16+
}
17+
return
1818
}

0 commit comments

Comments
 (0)