Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcc98c59

Browse files
author
Kyle Liu
authored
Merge pull request6boris#147 from kylesliu/develop
add 771 solution
2 parents279085b +34d23b4 commitcc98c59

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-
funcSolution(xbool)bool {
4-
returnx
3+
import (
4+
"strings"
5+
)
6+
7+
funccanConstruct(ransomNotestring,magazinestring)bool {
8+
for_,c:=rangeransomNote {
9+
idx:=strings.IndexRune(magazine,c)
10+
ifidx>-1 {
11+
magazine=magazine[:idx]+magazine[idx+1:]
12+
}else {
13+
returnfalse
14+
}
15+
}
16+
returntrue
17+
}
18+
funccanConstruct2(ransomNotestring,magazinestring)bool {
19+
m:=make(map[rune]int)
20+
21+
for_,v:=rangemagazine {
22+
m[v]++
23+
}
24+
for_,v:=rangeransomNote {
25+
ifm[v]==0 {
26+
returnfalse
27+
}
28+
m[v]--
29+
}
30+
returntrue
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
namestring
13-
inputsbool
13+
input1string
14+
input2string
1415
expectbool
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
fori,c:=rangecases {
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+
funcTestSolution2(t*testing.T) {
36+
//测试用例
37+
cases:= []struct {
38+
namestring
39+
input1string
40+
input2string
41+
expectbool
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+
fori,c:=rangecases {
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-
funcSolution(xbool)bool {
4-
returnx
3+
funcnumJewelsInStones(Jstring,Sstring)int {
4+
m,ans:=make(map[rune]int),0
5+
6+
for_,v:=rangeS {
7+
m[v]++
8+
}
9+
10+
for_,v:=rangeJ {
11+
ans+=m[v]
12+
}
13+
14+
returnans
15+
}
16+
17+
funcnumJewelsInStones2(Jstring,Sstring)int {
18+
ans:=0
19+
for_,i:=rangeJ {
20+
for_,j:=rangeS {
21+
ifi==j {
22+
ans++
23+
}
24+
25+
}
26+
}
27+
returnans
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
namestring
13-
inputsbool
14-
expectbool
13+
input1string
14+
input2string
15+
expectint
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
fori,c:=rangecases {
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+
funcTestSolution2(t*testing.T) {
34+
//测试用例
35+
cases:= []struct {
36+
namestring
37+
input1string
38+
input2string
39+
expectint
40+
}{
41+
{"TestCase","aA","aAAbbbb",3},
42+
{"TestCase","z","ZZ",0},
43+
}
44+
45+
//开始测试
46+
fori,c:=rangecases {
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)

[8]ページ先頭

©2009-2025 Movatter.jp