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

Commit4b74f07

Browse files
committed
add new files
1 parente9d3b4d commit4b74f07

13 files changed

+345
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"math"
5+
)
6+
7+
/**
8+
找到最大的和第二大的,如果最大的大于等于第二大的两倍,就找到了
9+
*/
10+
funcdominantIndex(nums []int)int {
11+
iflen(nums)==0 {
12+
return-1
13+
}
14+
iflen(nums)==1 {
15+
return0
16+
}
17+
max,sec:=math.MinInt32,math.MinInt32
18+
p:=-1
19+
fori:=0;i<len(nums);i++ {
20+
ifnums[i]>max {
21+
sec=max
22+
max=nums[i]
23+
p=i
24+
}elseifnums[i]>sec {
25+
sec=nums[i]
26+
}
27+
}
28+
ifmax>2*sec {
29+
returnp
30+
}
31+
return-1
32+
}

‎754. Reach a Number.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
funcreachNumber(targetint)int {
4+
iftarget<0 {
5+
target=-target
6+
}
7+
k:=0
8+
fortarget>0 {
9+
k++
10+
target-=k
11+
}
12+
iftarget%2==0 {
13+
returnk
14+
}
15+
returnk+1+k%2
16+
}
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"
4+
5+
typeTreeNodestruct {
6+
Valint
7+
Left*TreeNode
8+
Right*TreeNode
9+
}
10+
11+
/**
12+
先中序遍历,得到一个有序的数组
13+
*/
14+
15+
funchelper(root*TreeNode,l*[]int) {
16+
ifroot==nil {
17+
return
18+
}
19+
helper(root.Left,l)
20+
*l=append(*l,root.Val)
21+
helper(root.Right,l)
22+
}
23+
24+
funcminDiffInBST(root*TreeNode)int {
25+
ifroot==nil {
26+
return0
27+
}
28+
varl []int
29+
helper(root,&l)
30+
ret:=math.MaxInt32
31+
fori:=1;i<len(l);i++ {
32+
ifl[i]-l[i-1]<ret {
33+
ret=l[i]-l[i-1]
34+
}
35+
}
36+
returnret
37+
}

‎796. Rotate String.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
/**
4+
找到A中和B第一个字母相同的位置,然后重新拼接成一个新的字符串,判断是否相等
5+
*/
6+
funcrotateString(Astring,Bstring)bool {
7+
iflen(A)==0&&len(B)==0 {
8+
returntrue
9+
}
10+
iflen(A)==0||len(B)==0 {
11+
returnfalse
12+
}
13+
a,b:= []byte(A), []byte(B)
14+
fork,v:=rangea {
15+
ifv==b[0] {
16+
c:=a[k:]
17+
c=append(c,a[:k]...)
18+
ifstring(c)==string(b) {
19+
returntrue
20+
}
21+
}
22+
}
23+
returnfalse
24+
}

‎819. Most Common Word.go‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
)
7+
8+
funcmostCommonWord(paragraphstring,banned []string)string {
9+
m:=make(map[string]int)
10+
m1:=make(map[string]bool)
11+
for_,v:=rangebanned {
12+
m1[v]=true
13+
}
14+
p:=strings.ToLower(paragraph)
15+
re,_:=regexp.Compile("[,.!?;'']")
16+
p=re.ReplaceAllString(p," ")
17+
r:=strings.Split(p," ")
18+
for_,v:=ranger {
19+
if_,ok:=m[v];ok {
20+
m[v]++
21+
}else {
22+
if_,ok:=m1[v];!ok&&strings.Trim(v," ")!="" {
23+
m[v]=1
24+
}
25+
}
26+
}
27+
max:=0
28+
ret:=" "
29+
forv:=rangem {
30+
ifm[v]>max {
31+
max=m[v]
32+
ret=v
33+
}
34+
}
35+
returnret
36+
}

‎830. Positions of Large Groups.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
funclargeGroupPositions(Sstring) [][]int {
4+
ret:= [][]int{}
5+
i,j:=0,0
6+
fori<len(S) {
7+
forj<len(S)&&S[i]==S[j] {
8+
j++
9+
}
10+
ifj-i>=3 {
11+
tmp:= []int{i,j-1}
12+
ret=append(ret,tmp)
13+
}
14+
i=j
15+
}
16+
returnret
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
/**
4+
如果两头是0,开头和末尾就是距离
5+
如果中间是0,距离是0的个数/2
6+
取最大值
7+
*/
8+
funcmaxDistToClosest(seats []int)int {
9+
l:=len(seats)
10+
ret:= []int{}
11+
fork,v:=rangeseats {
12+
ifv==1 {
13+
ret=append(ret,k)
14+
}
15+
}
16+
m:=len(ret)
17+
pre:=ret[0]
18+
max:=mymax(pre,l-1-ret[m-1])
19+
for_,v:=rangeret {
20+
max=mymax(max, (v-pre)/2)
21+
pre=v
22+
}
23+
returnmax
24+
}
25+
26+
funcmymax(x,yint)int {
27+
ifx>y {
28+
returnx
29+
}
30+
returny
31+
}

‎872. Leaf-Similar Trees.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
/**
10+
先递归找到所有的叶子节点,再循环判断
11+
*/
12+
13+
funcleaf(root*TreeNode,ret*[]int) {
14+
ifroot==nil {
15+
return
16+
}
17+
ifroot.Left==nil&&root.Right==nil {
18+
*ret=append(*ret,root.Val)
19+
}
20+
leaf(root.Left,ret)
21+
leaf(root.Right,ret)
22+
}
23+
funcleafSimilar(root1*TreeNode,root2*TreeNode)bool {
24+
a,b:= []int{}, []int{}
25+
leaf(root1,&a)
26+
leaf(root2,&b)
27+
fork,v:=rangea {
28+
ifb[k]!=v {
29+
returnfalse
30+
}
31+
}
32+
returntrue
33+
}

‎876. Middle of the Linked List.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
typeListNodestruct {
4+
Valint
5+
Next*ListNode
6+
}
7+
8+
/**
9+
快慢指针,快指针走两步,满指针走一步
10+
*/
11+
funcmiddleNode(head*ListNode)*ListNode {
12+
ifhead==nil||head.Next==nil {
13+
returnhead
14+
}
15+
fast:=head
16+
slow:=head
17+
forfast!=nil {
18+
iffast.Next==nil {
19+
break
20+
}
21+
fast=fast.Next
22+
slow=slow.Next
23+
iffast.Next!=nil {
24+
fast=fast.Next
25+
}
26+
}
27+
returnslow
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
varret=TreeNode{Val:0}
10+
vartmp=&ret
11+
12+
funcincreasingBST(root*TreeNode)*TreeNode {
13+
ifroot==nil {
14+
returnroot
15+
}
16+
increasingBST(root.Left)
17+
tmp.Right=&TreeNode{
18+
Val:root.Val,
19+
}
20+
tmp=tmp.Right
21+
increasingBST(root.Right)
22+
returnret.Right
23+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp