Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

feat: use any instead of interface{} in golang #1937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
48 changes: 24 additions & 24 deletions lcci/04.12.Paths with Sum/Solution.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func pathSum(root *TreeNode, sum int) int {
cnt := map[int]int{0: 1}
var dfs func(*TreeNode, int) int
dfs = func(root *TreeNode, s int) int {
if root == nil {
return 0
}
s += root.Val
ans := cnt[s-sum]
cnt[s]++
ans += dfs(root.Left, s)
ans += dfs(root.Right, s)
cnt[s]--
return ans
}
return dfs(root, 0)
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func pathSum(root *TreeNode, sum int) int {
cnt := map[int]int{0: 1}
var dfs func(*TreeNode, int) int
dfs = func(root *TreeNode, s int) int {
if root == nil {
return 0
}
s += root.Val
ans := cnt[s-sum]
cnt[s]++
ans += dfs(root.Left, s)
ans += dfs(root.Right, s)
cnt[s]--
return ans
}
return dfs(root, 0)
}
10 changes: 5 additions & 5 deletions lcci/05.01.Insert Into Bits/Solution.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
func insertBits(N int, M int, i int, j int) int {
for k := i; k <= j; k++ {
N &= ^(1 << k)
}
return N | M<<i
func insertBits(N int, M int, i int, j int) int {
for k := i; k <= j; k++ {
N &= ^(1 << k)
}
return N | M<<i
}
58 changes: 29 additions & 29 deletions lcci/05.04.Closed Number/Solution.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
func findClosedNumbers(num int) []int {
ans := []int{-1, -1}
dirs := [3]int{0, 1, 0}
for p := 0; p < 2; p++ {
a, b := dirs[p], dirs[p+1]
x := num
for i := 1; i < 31; i++ {
if x>>i&1 == a && x>>(i-1)&1 == b {
x ^= 1 << i
x ^= 1 << (i - 1)
j, k := 0, i-2
for j < k {
for j < k && x>>j&1 == b {
j++
}
for j < k && x>>k&1 == a {
k--
}
if j < k {
x ^= 1 << j
x ^= 1 << k
}
}
ans[p] = x
break
}
}
}
return ans
func findClosedNumbers(num int) []int {
ans := []int{-1, -1}
dirs := [3]int{0, 1, 0}
for p := 0; p < 2; p++ {
a, b := dirs[p], dirs[p+1]
x := num
for i := 1; i < 31; i++ {
if x>>i&1 == a && x>>(i-1)&1 == b {
x ^= 1 << i
x ^= 1 << (i - 1)
j, k := 0, i-2
for j < k {
for j < k && x>>j&1 == b {
j++
}
for j < k && x>>k&1 == a {
k--
}
if j < k {
x ^= 1 << j
x ^= 1 << k
}
}
ans[p] = x
break
}
}
}
return ans
}
80 changes: 40 additions & 40 deletions lcci/08.01.Three Steps Problem/Solution.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
const mod = 1e9 + 7
func waysToStep(n int) (ans int) {
if n < 4 {
return int(math.Pow(2, float64(n-1)))
}
a := [][]int{{1, 1, 0}, {1, 0, 1}, {1, 0, 0}}
res := pow(a, n-4)
for _, x := range res[0] {
ans = (ans + x) % mod
}
return
}
func mul(a, b [][]int) [][]int {
m, n := len(a), len(b[0])
c := make([][]int, m)
for i := range c {
c[i] = make([]int, n)
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
for k := 0; k < len(b); k++ {
c[i][j] = (c[i][j] + a[i][k]*b[k][j]%mod) % mod
}
}
}
return c
}
func pow(a [][]int, n int) [][]int {
res := [][]int{{4, 2, 1}}
for n > 0 {
if n&1 == 1 {
res = mul(res, a)
}
a = mul(a, a)
n >>= 1
}
return res
const mod = 1e9 + 7

func waysToStep(n int) (ans int) {
if n < 4 {
return int(math.Pow(2, float64(n-1)))
}
a := [][]int{{1, 1, 0}, {1, 0, 1}, {1, 0, 0}}
res := pow(a, n-4)
for _, x := range res[0] {
ans = (ans + x) % mod
}
return
}

func mul(a, b [][]int) [][]int {
m, n := len(a), len(b[0])
c := make([][]int, m)
for i := range c {
c[i] = make([]int, n)
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
for k := 0; k < len(b); k++ {
c[i][j] = (c[i][j] + a[i][k]*b[k][j]%mod) % mod
}
}
}
return c
}

func pow(a [][]int, n int) [][]int {
res := [][]int{{4, 2, 1}}
for n > 0 {
if n&1 == 1 {
res = mul(res, a)
}
a = mul(a, a)
n >>= 1
}
return res
}
16 changes: 8 additions & 8 deletions lcci/08.05.Recursive Mulitply/Solution.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
func multiply(A int, B int) int {
if B == 1 {
return A
}
if B&1 == 1 {
return (multiply(A, B>>1) << 1) + A
}
return multiply(A, B>>1) << 1
func multiply(A int, B int) int {
if B == 1 {
return A
}
if B&1 == 1 {
return (multiply(A, B>>1) << 1) + A
}
return multiply(A, B>>1) << 1
}
40 changes: 20 additions & 20 deletions lcci/08.06.Hanota/Solution.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
func hanota(A []int, B []int, C []int) []int {
stk := []Task{{len(A), &A, &B, &C}}
for len(stk) > 0 {
task := stk[len(stk)-1]
stk = stk[:len(stk)-1]
if task.n == 1 {
*task.c = append(*task.c, (*task.a)[len(*task.a)-1])
*task.a = (*task.a)[:len(*task.a)-1]
} else {
stk = append(stk, Task{task.n - 1, task.b, task.a, task.c})
stk = append(stk, Task{1, task.a, task.b, task.c})
stk = append(stk, Task{task.n - 1, task.a, task.c, task.b})
}
}
return C
}
type Task struct {
n int
a, b, c *[]int
func hanota(A []int, B []int, C []int) []int {
stk := []Task{{len(A), &A, &B, &C}}
for len(stk) > 0 {
task := stk[len(stk)-1]
stk = stk[:len(stk)-1]
if task.n == 1 {
*task.c = append(*task.c, (*task.a)[len(*task.a)-1])
*task.a = (*task.a)[:len(*task.a)-1]
} else {
stk = append(stk, Task{task.n - 1, task.b, task.a, task.c})
stk = append(stk, Task{1, task.a, task.b, task.c})
stk = append(stk, Task{task.n - 1, task.a, task.c, task.b})
}
}
return C
}

type Task struct {
n int
a, b, c *[]int
}
50 changes: 25 additions & 25 deletions lcci/08.08.Permutation II/Solution.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
func permutation(S string) (ans []string) {
cs := []byte(S)
sort.Slice(cs, func(i, j int) bool { return cs[i] < cs[j] })
t := []byte{}
n := len(cs)
vis := make([]bool, n)
var dfs func(int)
dfs = func(i int) {
if i == n {
ans = append(ans, string(t))
return
}
for j := 0; j < n; j++ {
if vis[j] || (j > 0 && !vis[j-1] && cs[j] == cs[j-1]) {
continue
}
vis[j] = true
t = append(t, cs[j])
dfs(i + 1)
t = t[:len(t)-1]
vis[j] = false
}
}
dfs(0)
return
func permutation(S string) (ans []string) {
cs := []byte(S)
sort.Slice(cs, func(i, j int) bool { return cs[i] < cs[j] })
t := []byte{}
n := len(cs)
vis := make([]bool, n)
var dfs func(int)
dfs = func(i int) {
if i == n {
ans = append(ans, string(t))
return
}
for j := 0; j < n; j++ {
if vis[j] || (j > 0 && !vis[j-1] && cs[j] == cs[j-1]) {
continue
}
vis[j] = true
t = append(t, cs[j])
dfs(i + 1)
t = t[:len(t)-1]
vis[j] = false
}
}
dfs(0)
return
}
34 changes: 17 additions & 17 deletions lcci/08.13.Pile Box/Solution.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
func pileBox(box [][]int) (ans int) {
sort.Slice(box, func(i, j int) bool {
a, b := box[i], box[j]
return a[0] < b[0] || (a[0] == b[0] && b[1] < a[1])
})
n := len(box)
f := make([]int, n)
for i := 0; i < n; i++ {
for j := 0; j < i; j++ {
if box[j][1] < box[i][1] && box[j][2] < box[i][2] {
f[i] = max(f[i], f[j])
}
}
f[i] += box[i][2]
ans = max(ans, f[i])
}
return
func pileBox(box [][]int) (ans int) {
sort.Slice(box, func(i, j int) bool {
a, b := box[i], box[j]
return a[0] < b[0] || (a[0] == b[0] && b[1] < a[1])
})
n := len(box)
f := make([]int, n)
for i := 0; i < n; i++ {
for j := 0; j < i; j++ {
if box[j][1] < box[i][1] && box[j][2] < box[i][2] {
f[i] = max(f[i], f[j])
}
}
f[i] += box[i][2]
ans = max(ans, f[i])
}
return
}
Loading