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

Commit81f8652

Browse files
committed
add new files
1 parenta293e17 commit81f8652

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

‎677. Map Sum Pairs.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
/**
4+
这里实现了一个trie数
5+
*/
6+
typeTriestruct {
7+
charbyte
8+
endbool
9+
valueint
10+
childrenmap[byte]*Trie
11+
}
12+
13+
func (t*Trie)Insert(keystring,val,delint) {
14+
ifkey=="" {
15+
return
16+
}
17+
t.value+=val-del
18+
iflen(key)==1 {
19+
t.end=true
20+
return
21+
}
22+
ch,ok:=t.children[key[1]]
23+
if!ok {
24+
ch=&Trie{char:key[1],children:map[byte]*Trie{}}
25+
t.children[key[1]]=ch
26+
}
27+
(*ch).Insert(key[1:],val,del)
28+
}
29+
func (t*Trie)Find(keystring) (int,bool) {
30+
ifkey=="" {
31+
return0,false
32+
}
33+
iflen(key)==1 {
34+
returnt.value,t.end
35+
}
36+
ch,ok:=t.children[key[1]]
37+
if!ok {
38+
return0,false
39+
}
40+
returnch.Find(key[1:])
41+
}
42+
43+
typeMapSumstruct {
44+
triesmap[byte]*Trie
45+
}
46+
47+
funcConstructor()MapSum {
48+
returnMapSum{map[byte]*Trie{}}
49+
}
50+
func (this*MapSum)Insert(keystring,valint) {
51+
ift,ok:=this.tries[key[0]];ok {
52+
prev,ok:= (*t).Find(key)
53+
if!ok {
54+
prev=0
55+
}
56+
(*t).Insert(key,val,prev)
57+
}else {
58+
t=&Trie{char:key[0],children:map[byte]*Trie{}}
59+
t.char=key[0]
60+
this.tries[key[0]]=t
61+
(*t).Insert(key,val,0)
62+
}
63+
}
64+
65+
func (this*MapSum)Sum(prefixstring)int {
66+
ifprefix=="" {
67+
return0
68+
}
69+
t,ok:=this.tries[prefix[0]]
70+
if!ok {
71+
return0
72+
}
73+
val,_:=t.Find(prefix)
74+
returnval
75+
}

‎686. Repeated String Match.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import"strings"
4+
5+
funcrepeatedStringMatch(Astring,Bstring)int {
6+
iflen(A)==0&&len(B)==0 {
7+
return1
8+
}elseiflen(A)==0||len(B)==0 {
9+
return0
10+
}
11+
s:=A
12+
i:=0
13+
m:=len(B)/len(A)+2
14+
fori<m {
15+
ifstrings.Index(s,B)>-1 {
16+
returni+1
17+
}
18+
s+=A
19+
i++
20+
}
21+
return-1
22+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp