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

Commitf0eb0ff

Browse files
committed
add new files
1 parentdef75b2 commitf0eb0ff

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed

‎11. Container With Most Water.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
funcmaxArea(height []int)int {
4+
left,right:=0,len(height)-1
5+
maxArea:=0
6+
forleft<right {
7+
maxArea=max(maxArea,min(height[left],height[right])*(right-left))
8+
ifheight[left]<height[right] {
9+
left++
10+
}else {
11+
right--
12+
}
13+
}
14+
returnmaxArea
15+
}
16+
17+
funcmax(x,yint)int {
18+
ifx>y {
19+
returnx
20+
}
21+
returny
22+
}
23+
funcmin(x,yint)int {
24+
ifx>y {
25+
returny
26+
}
27+
returnx
28+
}

‎16. 3Sum Closest.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"math"
5+
"sort"
6+
)
7+
8+
functhreeSumClosest(nums []int,targetint)int {
9+
dist:=math.MaxInt32
10+
ret:=0
11+
iflen(nums)<3 {
12+
return0
13+
}
14+
sort.Ints(nums)
15+
l:=len(nums)
16+
fori:=0;i<l-2;i++ {
17+
ifi>0&&nums[i]==nums[i-1] {
18+
continue
19+
}
20+
begin,end:=i+1,l-1
21+
forbegin<end {
22+
sum:=nums[begin]+nums[end]+nums[i]
23+
ifsum<target {
24+
iftarget-sum<dist {
25+
dist=target-sum
26+
ret=sum
27+
}
28+
begin++
29+
}elseifsum>target {
30+
ifsum-target<dist {
31+
dist=sum-target
32+
ret=sum
33+
}
34+
end--
35+
}else {
36+
returnsum
37+
}
38+
}
39+
}
40+
returnret
41+
}

‎20. Valid Parentheses.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
/**
4+
用一个slice模拟stack
5+
遍历字符串,遇到左括号入栈,遇到右括号出战并判断
6+
最后判断栈是否为空
7+
*/
8+
9+
funcisValid(sstring)bool {
10+
str:= []byte(s)
11+
stack:= []byte{}
12+
for_,v:=rangestr {
13+
if'('==v||'{'==v||'['==v {
14+
stack=append(stack,v)
15+
}
16+
iflen(stack)==0 {
17+
returnfalse
18+
}
19+
if')'==v {
20+
ifstack[len(stack)-1]!='(' {
21+
returnfalse
22+
}else {
23+
stack=stack[:len(stack)-1]
24+
}
25+
}
26+
if'}'==v {
27+
ifstack[len(stack)-1]!='{' {
28+
returnfalse
29+
}else {
30+
stack=stack[:len(stack)-1]
31+
}
32+
}
33+
if']'==v {
34+
ifstack[len(stack)-1]!='[' {
35+
returnfalse
36+
}else {
37+
stack=stack[:len(stack)-1]
38+
}
39+
}
40+
}
41+
returnlen(stack)==0
42+
}

‎5. Longest Palindromic Substring.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
/**
4+
dp算法
5+
*/
6+
7+
funclongestPalindrome(sstring)string {
8+
str:= []byte(s)
9+
l:=len(str)
10+
ifl==0 {
11+
return""
12+
}
13+
dp:=make([][]bool,l)
14+
fork:=rangedp {
15+
dp[k]=make([]bool,l)
16+
}
17+
ret:= []byte{}
18+
max:=0
19+
fori:=l-1;i>=0;i-- {
20+
forj:=i;j<l;j++ {
21+
ifstr[i]==str[j]&& (j-i<=2||dp[i+1][j-1]) {
22+
dp[i][j]=true
23+
ifmax<j-i+1 {
24+
max=j-i+1
25+
ret=str[i :j+1]
26+
}
27+
}
28+
}
29+
}
30+
returnstring(ret)
31+
}

‎6. ZigZag Conversion.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
/**
4+
关键是要算出循环的长度:
5+
为 row + row -2
6+
*/
7+
funcconvert(sstring,numRowsint)string {
8+
ifnumRows==1 {
9+
returns
10+
}
11+
12+
item_len:=2*numRows-2//循环的长度
13+
res:=make([][]string,numRows,numRows)
14+
15+
forindex,v:=ranges {
16+
17+
mod:=index%item_len
18+
19+
ifmod<numRows {
20+
res[mod]=append(res[mod],string(v))
21+
}else {
22+
i:=numRows- (mod-numRows)-2
23+
res[i]=append(res[i],string(v))
24+
}
25+
}
26+
27+
varstrstring
28+
29+
for_,arr:=rangeres {
30+
for_,v:=rangearr {
31+
str+=v
32+
}
33+
}
34+
35+
returnstr
36+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp