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

Commit4a14518

Browse files
committed
add new files
1 parentbee5de7 commit4a14518

8 files changed

+178
-0
lines changed

‎287. Find the Duplicate Number.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
funcfindDuplicate(nums []int)int {
4+
left,right:=1,len(nums)-1
5+
mid:=left+ (right-left)/2
6+
forleft<right {
7+
c:=0
8+
mid=left+ (right-left)/2
9+
fori:=0;i<len(nums);i++ {
10+
ifnums[i]<=mid {
11+
c++
12+
}
13+
}
14+
ifc>mid {
15+
right=mid
16+
}else {
17+
left=mid+1
18+
}
19+
}
20+
returnleft
21+
}

‎344. Reverse String.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
funcreverseString(s []byte) {
4+
fori,j:=0,len(s)-1;i<j;i,j=i+1,j-1 {
5+
s[i],s[j]=s[j],s[i]
6+
}
7+
8+
}

‎349. Intersection of Two Arrays.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
funcintersection(nums1 []int,nums2 []int) []int {
4+
m:=make(map[int]bool)
5+
for_,v:=rangenums1 {
6+
m[v]=true
7+
}
8+
9+
ret:= []int{}
10+
for_,v:=rangenums2 {
11+
ifval,ok:=m[v];val&&ok {
12+
ret=append(ret,v)
13+
m[v]=false
14+
}
15+
}
16+
returnret
17+
}

‎350. Intersection of Two Arrays II.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
funcintersect(nums1 []int,nums2 []int) []int {
4+
m:=make(map[int]int)
5+
6+
for_,v:=rangenums1 {
7+
if_,ok:=m[v];ok {
8+
m[v]++
9+
}else {
10+
m[v]=1
11+
}
12+
}
13+
14+
ret:= []int{}
15+
16+
for_,v:=rangenums2 {
17+
ifval,ok:=m[v];val>0&&ok {
18+
ret=append(ret,v)
19+
m[v]--
20+
}
21+
}
22+
returnret
23+
}

‎382. Linked List Random Node.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import"math/rand"
4+
5+
typeListNodestruct {
6+
Valint
7+
Next*ListNode
8+
}
9+
10+
typeSolutionstruct {
11+
countint
12+
head*ListNode
13+
pool []int
14+
}
15+
16+
/** @param head The linked list's head.
17+
Note that the head is guaranteed to be not null, so it contains at least one node. */
18+
funcConstructor(head*ListNode)Solution {
19+
varsSolution
20+
s.head=head
21+
returns
22+
}
23+
24+
/** Returns a random node's value. */
25+
func (this*Solution)GetRandom()int {
26+
r:=rand.Intn(this.count+1)
27+
this.count++
28+
ifr==this.count-1&&this.head!=nil {
29+
t:=this.head
30+
this.head=this.head.Next
31+
this.pool=append(this.pool,t.Val)
32+
returnt.Val
33+
}else {
34+
returnthis.pool[r%len(this.pool)]
35+
}
36+
37+
}

‎389. Find the Difference.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
//预先把每个字母都装进hash
4+
funcfindTheDifference(sstring,tstring)byte {
5+
6+
m:=make(map[int32]int,256)
7+
8+
variint32
9+
fori=0;i<256;i++ {
10+
m[i]=0
11+
}
12+
13+
for_,v:=ranges {
14+
m[v]++
15+
}
16+
for_,v:=ranget {
17+
m[v]--
18+
ifm[v]<0 {
19+
returnbyte(v)
20+
}
21+
}
22+
return0
23+
}

‎404. Sum of Left Leaves.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
funcsumOfLeftLeaves(root*TreeNode)int {
10+
sum:=0
11+
ifroot!=nil {
12+
ifisLeaf(root.Left) {
13+
sum+=root.Left.Val
14+
}else {
15+
sum+=sumOfLeftLeaves(root.Left)
16+
}
17+
sum+=sumOfLeftLeaves(root.Right)
18+
}
19+
returnsum
20+
}
21+
22+
funcisLeaf(root*TreeNode)bool {
23+
ifroot==nil||root.Left!=nil||root.Right!=nil {
24+
returnfalse
25+
}
26+
returntrue
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
funccountSegments(sstring)int {
4+
5+
count:=1
6+
found:=false
7+
fori:=0;i<len(s);i++ {
8+
ifi>0&&s[i]==' '&&s[i+1]!=' ' {
9+
count++
10+
}
11+
ifs[i]!=' ' {
12+
found=true
13+
}
14+
ifi==len(s)-1&&s[i]==' ' {
15+
count--
16+
}
17+
}
18+
iffound {
19+
returncount
20+
}
21+
return0
22+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp