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

Commit7c8c236

Browse files
committed
add new files
1 parent782c3f4 commit7c8c236

11 files changed

+264
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
/**
10+
注意题目给的条件
11+
*/
12+
funcdfs(root*TreeNode,minint)int {
13+
ifroot==nil {
14+
return-1
15+
}
16+
ifroot.Val>min {
17+
returnroot.Val
18+
}
19+
left:=dfs(root.Left,min)
20+
right:=dfs(root.Right,min)
21+
ifleft==-1 {
22+
returnright
23+
}
24+
ifright==-1 {
25+
returnleft
26+
}
27+
ifleft<right {
28+
returnleft
29+
}
30+
returnright
31+
32+
}
33+
funcfindSecondMinimumValue(root*TreeNode)int {
34+
ifroot==nil {
35+
return-1
36+
}
37+
returndfs(root,root.Val)
38+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
funcfindLengthOfLCIS(nums []int)int {
4+
iflen(nums)<=1 {
5+
returnlen(nums)
6+
}
7+
/**
8+
ret保存最终结果,l保存临时结果
9+
*/
10+
ret,l:=1,1
11+
fori:=1;i<len(nums);i++ {
12+
ifnums[i]<=nums[i-1] {
13+
ifl>ret {
14+
ret=l
15+
}
16+
l=1
17+
}else {
18+
l++
19+
}
20+
ifret<l {
21+
ret=l
22+
}
23+
}
24+
returnret
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
/**
10+
根据BST的性质
11+
*/
12+
funcsearchBST(root*TreeNode,valint)*TreeNode {
13+
ifroot==nil {
14+
returnnil
15+
}
16+
ifroot.Val==val {
17+
returnroot
18+
}
19+
ifroot.Val>val {
20+
returnsearchBST(root.Left,val)
21+
}else {
22+
returnsearchBST(root.Right,val)
23+
}
24+
}

‎705. Design HashSet.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
/**
4+
用map实现
5+
*/
6+
typeMyHashSetstruct {
7+
smap[int]int
8+
}
9+
10+
funcConstructor()MyHashSet {
11+
ss:=make(map[int]int)
12+
h:=MyHashSet{
13+
s:ss,
14+
}
15+
returnh
16+
}
17+
18+
func (this*MyHashSet)Add(keyint) {
19+
this.s[key]=1
20+
}
21+
22+
func (this*MyHashSet)Remove(keyint) {
23+
delete(this.s,key)
24+
}
25+
26+
/** Returns true if this set contains the specified element */
27+
func (this*MyHashSet)Contains(keyint)bool {
28+
_,ok:=this.s[key]
29+
returnok
30+
}

‎706. Design HashMap.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
//TODO 效率待提升
4+
typeMyHashMapstruct {
5+
s []int
6+
}
7+
8+
/** Initialize your data structure here. */
9+
funcConstructor()MyHashMap {
10+
ss:=make([]int,1000000)
11+
fori:=0;i<len(ss);i++ {
12+
ss[i]=-1
13+
}
14+
returnMyHashMap{
15+
s:ss,
16+
}
17+
}
18+
19+
/** value will always be non-negative. */
20+
func (this*MyHashMap)Put(keyint,valueint) {
21+
this.s[key]=value
22+
}
23+
24+
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
25+
func (this*MyHashMap)Get(keyint)int {
26+
ifthis.s[key]!=-1 {
27+
returnthis.s[key]
28+
}
29+
return-1
30+
}
31+
32+
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
33+
func (this*MyHashMap)Remove(keyint) {
34+
this.s[key]=-1
35+
}

‎709. To Lower Case.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
functoLowerCase(strstring)string {
4+
s:= []byte(str)
5+
fori:=0;i<len(s);i++ {
6+
ifs[i]>='A'&&s[i]<='Z' {
7+
s[i]+='a'-'A'
8+
}
9+
}
10+
returnstring(s)
11+
}

‎720. Longest Word in Dictionary.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import"sort"
4+
5+
/**
6+
先排序
7+
*/
8+
funclongestWord(words []string)string {
9+
sort.Strings(words)
10+
m:=make(map[string]bool)
11+
ret:=""
12+
for_,w:=rangewords {
13+
iflen(w)==1||m[w[0:len(w)-1]]==true {
14+
iflen(ret)<len(w) {
15+
ret=w
16+
}
17+
m[w]=true
18+
}
19+
}
20+
returnret
21+
}

‎724. Find Pivot Index.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
/**
4+
trick:先计算总和,然后判断什么时候等于一半
5+
*/
6+
funcpivotIndex(nums []int)int {
7+
sum:=0
8+
fori:=0;i<len(nums);i++ {
9+
sum+=nums[i]
10+
}
11+
ifsum==nums[0] {
12+
return0
13+
}
14+
half:=0
15+
fori:=1;i<len(nums);i++ {
16+
half+=nums[i-1]
17+
ifsum-nums[i]-half==half {
18+
returni
19+
}
20+
}
21+
return-1
22+
}

‎728. Self Dividing Numbers.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
funcselfDividingNumbers(leftint,rightint) []int {
4+
ret:= []int{}
5+
fori:=left;i<=right;i++ {
6+
flag:=true
7+
fortmp:=i;flag&&tmp>0; {
8+
iftmp%10==0||i%(tmp%10)>0 {
9+
flag=false
10+
}
11+
tmp/=10
12+
}
13+
ifflag {
14+
ret=append(ret,i)
15+
}
16+
}
17+
returnret
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
/**
4+
二分查找
5+
*/
6+
funcnextGreatestLetter(letters []byte,targetbyte)byte {
7+
low,high:=0,len(letters)-1
8+
forlow<=high {
9+
mid:= (low+high)/2
10+
ifletters[mid]>target {
11+
high=mid-1
12+
}else {
13+
low=mid+1
14+
}
15+
}
16+
iflow>=len(letters) {
17+
returnletters[0]
18+
}else {
19+
returnletters[low]
20+
}
21+
}

‎746. Min Cost Climbing Stairs.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
funcminCostClimbingStairs(cost []int)int {
4+
l:=len(cost)
5+
t:=make([]int,l)
6+
t[0]=cost[0]
7+
t[1]=cost[1]
8+
fori:=2;i<l;i++ {
9+
t[i]=mymin(t[i-1]+cost[i],t[i-2]+cost[i])
10+
}
11+
returnmymin(t[l-1],t[l-2])
12+
}
13+
14+
funcmymin(x,yint)int {
15+
ifx>y {
16+
returny
17+
}
18+
returnx
19+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp