10000 Merge pull request #147 from kylesliu/develop · huxulm/awesome-golang-leetcode@cc98c59 · GitHub
[go: up one dir, main page]

Skip to content

Commit cc98c59

Browse files
author
Kyle Liu
authored
Merge pull request 6boris#147 from kylesliu/develop
add 771 solution
2 parents 279085b + 34d23b4 commit cc98c59

File tree

4 files changed

+120
-19
lines changed

4 files changed

+120
-19
lines changed

src/0383.Ransom-Note/Solution.go

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

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"strings"
5+
)
6+
7+
func canConstruct(ransomNote string, magazine string) bool {
8+
for _, c := range ransomNote {
9+
idx := strings.IndexRune(magazine, c)
10+
if idx > -1 {
11+
magazine = magazine[:idx] + magazine[idx+1:]
12+
} else {
13+
return false
14+
}
15+
}
16+
return true
17+
}
18+
func canConstruct2(ransomNote string, magazine string) bool {
19+
m := make(map[rune]int)
20+
21+
for _, v := range magazine {
22+
m[v]++
23+
}
24+
for _, v := range ransomNote {
25+
if m[v] == 0 {
26+
return false
27+
}
28+
m[v]--
29+
}
30+
return true
531
}

src/0383.Ransom-Note/Solution_test.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,49 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
input1 string
14+
input2 string
1415
expect bool
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase", "a", "b", false},
18+
{"TestCase", "aa", "ab", false},
19+
{"TestCase", "aa", "aab", true},
20+
{"TestCase", "aab", "baa", true},
1921
}
2022

2123
// 开始测试
2224
for i, c := range cases {
2325
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
26+
got := canConstruct(c.input1, c.input2)
2527
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
28+
t.Fatalf("expected: %v, but got: %v, with input1: %v input2: %v",
29+
c.expect, got, c.input1, c.input2)
30+
}
31+
})
32+
}
33+
}
34+
35+
func TestSolution2(t *testing.T) {
36+
// 测试用例
37+
cases := []struct {
38+
name string
39+
input1 string
40+
input2 string
41+
expect bool
42+
}{
43+
{"TestCase", "a", "b", false},
44+
{"TestCase", "aa", "ab", false},
45+
{"TestCase", "aa", "aab", true},
46+
{"TestCase", "aab", "baa", true},
47+
}
48+
49+
// 开始测试
50+
for i, c := range cases {
51+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
52+
got := canConstruct2(c.input1, c.input2)
53+
if !reflect.DeepEqual(got, c.expect) {
54+
t.Fatalf("expected: %v, but got: %v, with input1: %v input2: %v",
55+
c.expect, got, c.input1, c.input2)
2856
}
2957
})
3058
}
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