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

Commit34d23b4

Browse files
committed
add 383 solution
1 parentc7ec27b commit34d23b4

File tree

2 files changed

+63
-9
lines changed

2 files changed

+63
-9
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
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp