8000 add 771 solution · reverse/awesome-golang-leetcode@c7ec27b · GitHub
[go: up one dir, main page]

Skip to content

Commit c7ec27b

Browse files
committed
add 771 solution
1 parent 44d8039 commit c7ec27b

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed
Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func numJewelsInStones(J string, S string) int {
4+
m, ans := make(map[rune]int), 0
5+
6+
for _, v := range S {
7+
m[v]++
8+
}
9+
10+
for _, v := range J {
11+
ans += m[v]
12+
}
13+
14+
return ans
15+
}
16+
17+
func numJewelsInStones2(J string, S string) int {
18+
ans := 0
19+
for _, i := range J {
20+
for _, j := range S {
21+
if i == j {
22+
ans++
23+
}
24+
25+
}
26+
}
27+
return ans
528
}

src/0771.Jewels-and-Stones/Solution_test.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,45 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
input1 string
14+
input2 string
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase", "aA", "aAAbbbb", 3},
18+
{"TestCase", "z", "ZZ", 0},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := numJewelsInStones(c.input1, c.input2)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with input1: %v input2: %v",
27+
c.expect, got, c.input1, c.input1)
28+
}
29+
})
30+
}
31+
}
32+
33+
func TestSolution2(t *testing.T) {
34+
// 测试用例
35+
cases := []struct {
36+
name string
37+
input1 string
38+
input2 string
39+
expect int
40+
}{
41+
{"TestCase", "aA", "aAAbbbb", 3},
42+
{"TestCase", "z", "ZZ", 0},
43+
}
44+
45+
// 开始测试
46+
for i, c := range cases {
47+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
48+
got := numJewelsInStones2(c.input1, c.input2)
49+
if !reflect.DeepEqual(got, c.expect) {
50+
t.Fatalf("expected: %v, but got: %v, with input1: %v input2: %v",
51+
c.expect, got, c.input1, c.input1)
2852
}
2953
})
3054
}

0 commit comments

Comments
 (0)
0