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

Commit53822c9

Browse files
committed
add new files
1 parenta4afee5 commit53822c9

5 files changed

+183
-0
lines changed

‎220. Contains Duplicate III.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
/**
4+
双重循环,维持一个滑动窗口
5+
*/
6+
7+
funccontainsNearbyAlmostDuplicate(nums []int,kint,tint)bool {
8+
iflen(nums)==0 {
9+
returnfalse
10+
}
11+
fori:=1;i<len(nums);i++ {
12+
j:=0
13+
ifi-k>0 {
14+
j=i-k
15+
}
16+
for ;j>=0&&j<i;j++ {
17+
ifabs(nums[i]-nums[j])<=t {
18+
returntrue
19+
}
20+
}
21+
}
22+
returnfalse
23+
}
24+
funcabs(xint)int {
25+
ifx>=0 {
26+
returnx
27+
}
28+
return-x
29+
}

‎227. Basic Calculator II.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 (
4+
"unicode"
5+
)
6+
7+
/**
8+
需要处理的就是乘除的情况
9+
设置一个栈,如果是+就直接入栈,如果是-,就负数入栈
10+
如果是乘或者除,就从栈里取出最后一个元素,运算之后入栈
11+
最后从栈里取出所有元素相加
12+
需要注意的是处理多位数字的情况
13+
*/
14+
15+
funccalculate(sstring)int {
16+
num:=0
17+
st:= []int{}
18+
varsignbyte
19+
sign='+'
20+
fori:=0;i<len(s);i++ {
21+
ifunicode.IsDigit(rune(s[i])) {//如果是多位数字
22+
num=num*10+int(s[i]-'0')
23+
}
24+
//如果当前字符是运算符,或者是最后一个字符(一定是数字)
25+
ifs[i]!=' '&&!unicode.IsDigit(rune(s[i]))||i==len(s)-1 {
26+
switchsign {
27+
case'+':
28+
case'-':
29+
num=-num
30+
case'*':
31+
num=st[len(st)-1]*num
32+
st=st[:len(st)-1]
33+
case'/':
34+
num=st[len(st)-1]/num
35+
st=st[:len(st)-1]
36+
}
37+
sign=s[i]
38+
st=append(st,num)
39+
num=0
40+
}
41+
}
42+
for_,v:=rangest {
43+
num+=v
44+
}
45+
returnnum
46+
}

‎229. Majority Element II.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"math"
5+
)
6+
7+
/**
8+
用两个变量记录出现最多的两个元素(大于1/3的元素不可能出现3个)
9+
然后统计出现这两个元素的个数,符合条件就加入返回值中
10+
*/
11+
funcmajorityElement(nums []int) []int {
12+
l:=len(nums)
13+
ifl==0 {
14+
return []int{}
15+
}
16+
ifl==1 {
17+
returnnums
18+
}
19+
m1,m2:=nums[0],math.MaxInt32
20+
c1,c2:=1,0
21+
fori:=1;i<l;i++ {
22+
ifnums[i]==m1 {
23+
c1++
24+
}elseifnums[i]==m2 {
25+
c2++
26+
}elseifc1==0 {
27+
m1=nums[i]
28+
c1=1
29+
}elseifc2==0 {
30+
m2=nums[i]
31+
c2=1
32+
}else {
33+
c1--
34+
c2--
35+
}
36+
}
37+
t1,t2:=0,0
38+
for_,v:=rangenums {
39+
ifv==m1 {
40+
t1++
41+
}
42+
ifv==m2 {
43+
t2++
44+
}
45+
}
46+
ret:= []int{}
47+
ift1>l/3 {
48+
ret=append(ret,m1)
49+
}
50+
ift2>l/3 {
51+
ret=append(ret,m2)
52+
}
53+
returnret
54+
}

‎230. Kth Smallest Element in a BST.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
/**
10+
先计算左子树中节点的个数
11+
如果左子树节点个数大于k-1,那么答案在左子树中
12+
如果个数小于k-1,就在右子树中
13+
如果刚好相等,就直接返回root节点的值
14+
*/
15+
16+
funckthSmallest(root*TreeNode,kint)int {
17+
left:=countLeaf(root.Left)
18+
ifleft==k-1 {
19+
returnroot.Val
20+
}elseifleft>k-1 {
21+
returnkthSmallest(root.Left,k)
22+
}else {
23+
returnkthSmallest(root.Right,k-left-1)
24+
}
25+
}
26+
27+
funccountLeaf(root*TreeNode)int {
28+
ifroot==nil {
29+
return0
30+
}
31+
returncountLeaf(root.Left)+countLeaf(root.Right)+1
32+
}

‎240. Search a 2D Matrix II.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+
从矩阵右上角开始遍历,如果大于target,则列数减一,如果小于target,则行数加一
5+
*/
6+
funcsearchMatrix(matrix [][]int,targetint)bool {
7+
iflen(matrix)==0||len(matrix[0])==0 {
8+
returnfalse
9+
}
10+
m,n:=len(matrix),len(matrix[0])
11+
i,j:=0,n-1
12+
fori<m&&j>=0 {
13+
ifmatrix[i][j]<target {
14+
i++
15+
}elseifmatrix[i][j]>target {
16+
j--
17+
}else {
18+
returntrue
19+
}
20+
}
21+
returnfalse
22+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp