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

Commit455af2b

Browse files
committed
add new files
1 parent36ab942 commit455af2b

10 files changed

+216
-0
lines changed

‎557. Reverse Words in a String III.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import"strings"
4+
5+
funcreverseWords(sstring)string {
6+
sp:=strings.Split(s," ")
7+
ret:=""
8+
fori:=0;i<len(sp);i++ {
9+
t:=make([]byte,len(sp[i]))
10+
forleft,right:=0,len(sp[i])-1;left<=right;left,right=left+1,right-1 {
11+
t[left],t[right]=sp[i][right],sp[i][left]
12+
}
13+
ret+=string(t)
14+
ifi!=len(sp)-1 {
15+
ret+=" "
16+
}
17+
}
18+
returnret
19+
20+
}

‎561. Array Partition I.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import"sort"
4+
5+
//先排序,然后取偶数位置的数相加,就是答案
6+
funcarrayPairSum(nums []int)int {
7+
sort.Ints(nums)
8+
ret:=0
9+
fori:=0;i<len(nums);i+=2 {
10+
ret+=nums[i]
11+
}
12+
returnret
13+
}

‎563. Binary Tree Tilt.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import"math"
4+
5+
typeTreeNodestruct {
6+
Valint
7+
Left*TreeNode
8+
Right*TreeNode
9+
}
10+
11+
varsum=0
12+
13+
funcfindTilt(root*TreeNode)int {
14+
helper(root)
15+
returnsum
16+
}
17+
18+
funchelper(root*TreeNode)int {
19+
ifroot==nil {
20+
return0
21+
}
22+
left:=helper(root.Left)
23+
right:=helper(root.Right)
24+
sum+=int(math.Abs(float64(left)-float64(right)))
25+
returnroot.Val+left+right
26+
}

‎572. Subtree of Another Tree.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
funchelper(s,t*TreeNode)bool {
10+
ifs==nil&&t==nil {
11+
returntrue
12+
}
13+
ifs==nil||t==nil {
14+
returnfalse
15+
}
16+
ifs.Val!=t.Val {
17+
returnfalse
18+
}
19+
returnhelper(s.Left,t.Left)&&helper(s.Right,t.Right)
20+
}
21+
funcisSubtree(s*TreeNode,t*TreeNode)bool {
22+
ifs==nil {
23+
returnfalse
24+
}
25+
ifhelper(s,t) {
26+
returntrue
27+
}
28+
returnisSubtree(s.Left,t)||isSubtree(s.Right,t)
29+
}

‎575. Distribute Candies.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
//当糖果的种类数目大于妹妹应分得的糖果数目时,妹妹所能分得的最大糖果种类数为妹妹应分得的糖果数,反之,则为糖果的种类数。
4+
funcdistributeCandies(candies []int)int {
5+
m:=make(map[int]int)
6+
for_,v:=rangecandies {
7+
if_,ok:=m[v];!ok {
8+
m[v]=1
9+
}
10+
}
11+
s:=len(candies)/2
12+
t:=len(m)
13+
ift>s {
14+
returns
15+
}else {
16+
returnt
17+
}
18+
19+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
/**
4+
来看这个例子:1 3 5 7 2 4 5 6
5+
6+
  a. 当我们找到第一个违反ascending 排序的数字 2的时候,我们不能是仅仅把beg 标记为2的前面一个数字7,而是要一直往前,找到一个合适的位置,找到在最前面位置的比2大的数字,这里是3。
7+
8+
  b. 同样的,为了找end, 那么我们要从7的后面开始找,一直找到一个最后面位置的比7小的数字,这里是6。
9+
*/
10+
funcfindUnsortedSubarray(nums []int)int {
11+
12+
n:=len(nums)
13+
begin,end:=-1,-2
14+
min,max:=nums[n-1],nums[0]
15+
fori:=0;i<n;i++ {
16+
max=mymax(max,nums[i])
17+
min=mymin(min,nums[n-1-i])
18+
ifnums[i]<max {
19+
end=i
20+
}
21+
ifnums[n-1-i]>min {
22+
begin=n-1-i
23+
}
24+
}
25+
returnend-begin+1
26+
}
27+
28+
//使用自定义的max、min,速度会快很多
29+
funcmymin(x,yint)int {
30+
ifx>y {
31+
returny
32+
}
33+
returnx
34+
}
35+
funcmymax(x,yint)int {
36+
ifx>y {
37+
returnx
38+
}
39+
returny
40+
}

‎605. Can Place Flowers.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
funccanPlaceFlowers(flowerbed []int,nint)bool {
4+
cnt:=0
5+
fori:=0;cnt<n&&i<len(flowerbed);i++ {
6+
ifi>0&&flowerbed[i-1]==1 {
7+
continue
8+
}
9+
ifi<len(flowerbed)-1&&flowerbed[i+1]==1 {
10+
continue
11+
}
12+
ifflowerbed[i]!=0 {
13+
continue
14+
}
15+
flowerbed[i]=1
16+
cnt++
17+
ifi<len(flowerbed)-1 {
18+
flowerbed[i+1]=-1
19+
}
20+
}
21+
returncnt>=n
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import"strconv"
4+
5+
functree2str(t*TreeNode)string {
6+
ift==nil {
7+
return""
8+
}
9+
ift.Left==nil&&t.Right==nil {
10+
returnstrconv.Itoa(t.Val)
11+
}
12+
ift.Right==nil {
13+
returnstrconv.Itoa(t.Val)+"("+tree2str(t.Left)+")"
14+
}
15+
returnstrconv.Itoa(t.Val)+"("+tree2str(t.Left)+")("+tree2str(t.Right)+")"
16+
}

‎617. Merge Two Binary Trees.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
funcmergeTrees(t1*TreeNode,t2*TreeNode)*TreeNode {
4+
ift1==nil {
5+
returnt2
6+
}
7+
ift2==nil {
8+
returnt1
9+
}
10+
t1.Left=mergeTrees(t1.Left,t2.Left)
11+
t1.Right=mergeTrees(t1.Right,t2.Right)
12+
t1.Val+=t2.Val
13+
returnt1
14+
}

‎633. Sum of Square Numbers.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import"math"
4+
5+
//从0到sqrt(c)之间进行遍历,检测剩下的数开方后是不是整数
6+
funcjudgeSquareSum(cint)bool {
7+
limit:=int(math.Sqrt(float64(c)))
8+
fori:=0;i<=limit;i++ {
9+
k:=c-i*i
10+
tmp:=int(math.Sqrt(float64(k)))
11+
iftmp*tmp==k {
12+
returntrue
13+
}
14+
15+
}
16+
returnfalse
17+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp