Skip to content

Commit bc6f3ea

Browse files
committed
<添加><算法><宝石包涵>
1 parent 1391bed commit bc6f3ea

File tree

2 files changed

+138
-8
lines changed

2 files changed

+138
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。
8+
//
9+
// J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。
10+
//
11+
// 示例 1:
12+
//
13+
// 输入: J = "aA", S = "aAAbbbb"
14+
// 输出: 3
15+
// 示例 2:
16+
//
17+
// 输入: J = "z", S = "ZZ"
18+
// 输出: 0
19+
// 注意:
20+
//
21+
// S 和 J 最多含有50个字母。
22+
// J 中的字符不重复。
23+
24+
func numJewelsInStones(J string, S string) int {
25+
m := make(map[byte]int)
26+
27+
for _, v := range S {
28+
m[byte(v)]++
29+
}
30+
31+
sum := 0
32+
for _, v := range J {
33+
sum += m[byte(v)]
34+
}
35+
return sum
36+
}
37+
38+
func main() {
39+
J := "aA"
40+
S := "aAAbbbb"
41+
i := numJewelsInStones(J, S)
42+
fmt.Println("宝石数量:", i)
43+
}
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,116 @@
11
package main
22

3+
import (
4+
"fmt"
5+
)
36

4-
// 给定两个大小为 m 和 n 的有序数组 nums1nums2
7+
// 给定两个大小为 m 和 n 的有序数组 AB
58
//
69
// 请找出这两个有序数组的中位数。要求算法的时间复杂度为 O(log (m+n)) 。
710
//
8-
// 你可以假设 nums1nums2 不同时为空。
11+
// 你可以假设 AB 不同时为空。
912
//
1013
// 示例 1:
1114
//
12-
// nums1 = [1, 3]
13-
// nums2 = [2]
15+
// A = [1, 3]
16+
// B = [2]
1417
//
1518
// 中位数是 2.0
1619
// 示例 2:
1720
//
18-
// nums1 = [1, 2]
19-
// nums2 = [3, 4]
21+
// A = [1, 2]
22+
// B = [3, 4]
2023
//
2124
// 中位数是 (2 + 3)/2 = 2.5
2225

2326
func main() {
27+
f := findMedianSortedArrays([]int{1, 2}, []int{3, 4})
28+
fmt.Println(f)
29+
}
30+
// 简单方法 TODO 测试效率不高 有时间压测下
31+
// func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
32+
// var nums []int
33+
// var f float64
34+
// for i := 0; i < len(nums2); i++ {
35+
// nums1 = append(nums1, nums2[i])
36+
// }
37+
// nums = nums1
38+
// sort.Ints(nums)
39+
// if len(nums)%2 != 0 {
40+
// index := float64(len(nums)) / 2
41+
// index = math.Floor(index + 0.5)
42+
// f = float64(nums[int(index)-1])
43+
// } else {
44+
// index := len(nums) / 2
45+
// f = float64(nums[index]+nums[index-1]) / 2
46+
// }
47+
// return f
48+
// }
49+
50+
func findMedianSortedArrays(A []int, B []int) float64 {
51+
m := len(A)
52+
n := len(B)
53+
54+
if m > n { // to ensure A <= B
55+
temp := A
56+
A = B
57+
B = temp
58+
59+
tmp := m
60+
m = n
61+
n = tmp
62+
}
63+
iMin := 0
64+
iMax := m
65+
halfLen := (m + n + 1) / 2
66+
for {
67+
if iMin > iMax {
68+
break
69+
}
70+
i := (iMin + iMax) / 2
71+
j := halfLen - i
72+
if i < iMax && B[j-1] > A[i] {
73+
iMin = i + 1 // i is too small
74+
} else if i > iMin && A[i-1] > B[j] {
75+
iMax = i - 1 // i is too big
76+
} else { // i is perfect
77+
maxLeft := 0
78+
if i == 0 {
79+
maxLeft = B[j-1]
80+
} else if j == 0 {
81+
maxLeft = A[i-1]
82+
} else {
83+
maxLeft = Max(A[i-1], B[j-1])
84+
}
85+
if (m+n)%2 == 1 {
86+
return float64(maxLeft)
87+
}
2488

89+
minRight := 0
90+
if i == m {
91+
minRight = B[j]
92+
} else if j == n {
93+
minRight = A[i]
94+
} else {
95+
minRight = Min(B[j], A[i])
96+
}
97+
98+
return float64(maxLeft+minRight) / 2.0
99+
}
100+
}
101+
return 0
25102
}
26103

27-
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
104+
func Max(x, y int) int {
105+
if x > y {
106+
return x
107+
}
108+
return y
109+
}
28110

29-
}
111+
func Min(x, y int) int {
112+
if x < y {
113+
return x
114+
}
115+
return y
116+
}

0 commit comments

Comments
 (0)