Skip to content

Commit 4a2cfab

Browse files
committed
feat(daily): base 7
1 parent 8d74653 commit 4a2cfab

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

0004.median-of-two-sorted-arrays/main.go

+36-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,45 @@ func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
88
m := len(nums1)
99
n := len(nums2)
1010
k := (m + n) / 2
11-
fmt.Println(m, n, k, k/2-1)
11+
//fmt.Println(k, k+1)
12+
//fmt.Println(findKth(nums1, nums2, k))
13+
fmt.Println(findKth(nums1, nums2, k+1))
14+
1215
return 0
1316
}
1417

18+
func findKth(nums1 []int, nums2 []int, k int) int {
19+
if k == 1 {
20+
n10 := nums1[0]
21+
n20 := nums2[0]
22+
if n10 < n20 {
23+
return n10
24+
}
25+
return n20
26+
}
27+
28+
removeNum := k / 2
29+
index := removeNum - 1
30+
31+
if len(nums1) <= removeNum {
32+
fmt.Println(nums1, nums2, k, removeNum)
33+
return nums2[k-1]
34+
}
35+
if len(nums2) <= removeNum {
36+
return nums1[k-1]
37+
}
38+
39+
if nums1[index] < nums2[index] {
40+
nums1 = nums1[removeNum:]
41+
} else {
42+
nums2 = nums2[removeNum:]
43+
}
44+
45+
return findKth(nums1, nums2, k-removeNum)
46+
}
47+
1548
func main() {
16-
nums1 := []int{1, 3, 4, 9}
17-
nums2 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
49+
nums1 := []int{1, 2, 3, 4, 5}
50+
nums2 := []int{6, 7, 8, 9, 10}
1851
findMedianSortedArrays(nums1, nums2)
1952
}

0504.base-7/main.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func convertToBase7(num int) string {
6+
if num == 0 {
7+
return "0"
8+
}
9+
isNegative := num < 0
10+
if isNegative {
11+
num = 0 - num
12+
}
13+
s := ""
14+
for {
15+
if num == 0 {
16+
break
17+
}
18+
digit := num % 7
19+
s = string(byte(digit+48)) + s
20+
num = num / 7
21+
}
22+
if isNegative {
23+
s = "-" + s
24+
}
25+
return s
26+
}
27+
28+
func main() {
29+
num := -7
30+
b7 := convertToBase7(num)
31+
fmt.Println(b7)
32+
}

0 commit comments

Comments
 (0)