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

Commite0b5cda

Browse files
committed
add new files
1 parent4a14518 commite0b5cda

7 files changed

+158
-1
lines changed

‎303. Range Sum Query - Immutable.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
typeNumArraystruct {
4+
nums []int
5+
}
6+
7+
funcConstructor(nums []int)NumArray {
8+
nn:= []int{}
9+
iflen(nums)<=1 {
10+
nn=nums
11+
}else {
12+
nn=append(nn,nums[0])
13+
fori:=1;i<len(nums);i++ {
14+
nn=append(nn,nums[i]+nn[i-1])
15+
}
16+
}
17+
returnNumArray{
18+
nums:nn,
19+
}
20+
}
21+
22+
func (this*NumArray)SumRange(iint,jint)int {
23+
ifi==0 {
24+
returnthis.nums[j]
25+
}else {
26+
returnthis.nums[j]-this.nums[i-1]
27+
}
28+
}
29+
30+
//
31+
//func main() {
32+
//r := []int{-2, 0, 3, -5, 2, -1}
33+
//a := Constructor(r)
34+
//b := a.SumRange(0, 2)
35+
//fmt.Println(b)
36+
//}

‎307. Range Sum Query - Mutable.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import"github.com/olivere/elastic"
4+
5+
typeNumArraystruct {
6+
n []int
7+
}
8+
9+
funcConstructor(nums []int)NumArray {
10+
n:= []int{}
11+
iflen(nums)<=1 {
12+
n=nums
13+
}else {
14+
n=append(n,nums[0])
15+
fori:=1;i<len(nums);i++ {
16+
n=append(n,nums[i]+n[i-1])
17+
}
18+
}
19+
returnNumArray{
20+
n:n,
21+
}
22+
}
23+
24+
func (this*NumArray)Update(iint,valint) {
25+
l:=len(this.n)
26+
ifi<0||i>l-1 {
27+
return
28+
}
29+
vardiffint
30+
ifi==0 {
31+
diff=val-this.n[0]
32+
}else {
33+
diff=val- (this.n[i]-this.n[i-1])
34+
}
35+
forj:=i;j<l;j++ {
36+
this.n[j]+=diff
37+
}
38+
}
39+
40+
func (this*NumArray)SumRange(iint,jint)int {
41+
ifi==0 {
42+
returnthis.n[j]
43+
}else {
44+
returnthis.n[j]-this.n[i-1]
45+
}
46+
}

‎345. Reverse Vowels of a String.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
funcreverseVowels(sstring)string {
4+
t:= []byte(s)
5+
m:=make(map[byte]bool)
6+
m['o'],m['e'],m['a'],m['i'],m['u']=true,true,true,true,true
7+
m['O'],m['E'],m['A'],m['I'],m['U']=true,true,true,true,true
8+
fori,j:=0,len(t)-1;i<j;i,j=i+1,j-1 {
9+
fori<j&&!m[t[i]] {
10+
i++
11+
}
12+
forj>i&&!m[t[j]] {
13+
j--
14+
}
15+
ifi>=j {
16+
break
17+
}
18+
t[i],t[j]=t[j],t[i]
19+
}
20+
returnstring(t)
21+
22+
}

‎383. Ransom Note.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
//一个入hash表,查另一个
4+
funccanConstruct(ransomNotestring,magazinestring)bool {
5+
m:=make(map[rune]int)
6+
for_,v:=rangemagazine {
7+
m[v]++
8+
}
9+
for_,v:=rangeransomNote {
10+
c,ok:=m[v]
11+
ifok==false||c==0 {
12+
returnfalse
13+
}
14+
m[v]--
15+
}
16+
returntrue
17+
}

‎409. Longest Palindrome.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
funclongestPalindrome(sstring)int {
4+
count:=make([]int,128)
5+
for_,c:=ranges {
6+
count[c]++
7+
}
8+
ans:=0
9+
for_,v:=rangecount {
10+
ans+=v/2*2
11+
ifans%2==0&&v%2==1 {
12+
ans++
13+
}
14+
}
15+
returnans
16+
}

‎434. Number of Segments in a String.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ func countSegments(s string) int {
55
count:=1
66
found:=false
77
fori:=0;i<len(s);i++ {
8-
ifi>0&&s[i]==' '&&s[i+1]!=' ' {
8+
ifi>0&&s[i]==' '&&s[i-1]!=' ' {
99
count++
1010
}
1111
ifs[i]!=' ' {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
funcfindDuplicates(nums []int) []int {
4+
5+
m:=make(map[int]int)
6+
for_,v:=rangenums {
7+
if_,ok:=m[v];ok {
8+
m[v]++
9+
}else {
10+
m[v]=1
11+
}
12+
}
13+
ret:= []int{}
14+
fork,v:=rangem {
15+
ifv==2 {
16+
ret=append(ret,k)
17+
}
18+
}
19+
returnret
20+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp