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

Commitdfc4c44

Browse files
authored
Merge pull request6boris#114 from kylesliu/develop
update 241 problem solution
2 parents68d2473 +245ae98 commitdfc4c44

File tree

3 files changed

+87
-7
lines changed

3 files changed

+87
-7
lines changed
Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,55 @@
11
package Solution
22

3+
import"strconv"
4+
35
funcdiffWaysToCompute(inputstring) []int {
4-
return []int{}
6+
returnways(input,map[string][]int{})
7+
}
8+
9+
funccalculate(a,bint,operaterune)int {
10+
switchoperate {
11+
case'+':
12+
returna+b
13+
case'-':
14+
returna-b
15+
case'*':
16+
returna*b
17+
default:
18+
panic("operate not exist")
19+
}
20+
}
21+
22+
funcways(inputstring,cachemap[string][]int) []int {
23+
if_,ok:=cache[input];ok {
24+
returncache[input]
25+
}
26+
27+
ans:= []int{}
28+
29+
fori:=0;i<len(input);i++ {
30+
ch:=input[i]
31+
32+
ifch=='+'||ch=='-'||ch=='*' {
33+
left:=input[:i]
34+
right:=input[i+1:]
35+
36+
l:=ways(left,cache)
37+
r:=ways(right,cache)
38+
39+
for_,a:=rangel {
40+
for_,b:=ranger {
41+
ans=append(ans,calculate(a,b,rune(ch)))
42+
}
43+
}
44+
}
45+
}
46+
47+
iflen(ans)==0 {
48+
number,_:=strconv.Atoi(input)
49+
ans=append(ans,number)
50+
}
51+
52+
cache[input]=ans
53+
54+
returnans
555
}

‎src/0241.Different-Ways-to-Add-Parentheses/Solution_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package Solution
22

33
import (
4-
"reflect"
54
"strconv"
65
"testing"
76
)
@@ -21,9 +20,17 @@ func TestSolution(t *testing.T) {
2120
fori,c:=rangecases {
2221
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
2322
got:=diffWaysToCompute(c.inputs)
24-
if!reflect.DeepEqual(got,c.expect) {
25-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
26-
c.expect,got,c.inputs)
23+
iflen(got)!=len(c.expect) {
24+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",c.expect,got,c.inputs)
25+
}
26+
m:=make(map[int]int)
27+
forv:=rangegot {
28+
m[v]++
29+
}
30+
forv:=rangec.expect {
31+
if_,ok:=m[v];!ok {
32+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",c.expect,got,c.inputs)
33+
}
2734
}
2835
})
2936
}

‎src/0621.Task-Scheduler/Solution.go

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

3+
import"sort"
34

4-
5+
funcmax(a,bint)int {
6+
ifa>b {
7+
returna
8+
}
9+
returnb
10+
}
511

612
funcleastInterval(tasks []byte,nint)int {
7-
return0
13+
tmp:=make([]int,26)
14+
temp:= []int{}
15+
p:=1
16+
for_,v:=rangetasks {
17+
tmp[v-'A']++
18+
}
19+
for_,v:=rangetmp {
20+
temp=append(temp,v)
21+
}
22+
sort.Slice(temp,func(a,bint)bool {
23+
returntemp[a]>temp[b]
24+
})
25+
fori:=1;i<len(temp);i++ {
26+
iftemp[0]==temp[i] {
27+
p++
28+
}
29+
}
30+
returnmax((n+1)*(temp[0]-1)+p,len(tasks))
831
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp