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

Commit4d8602f

Browse files
committed
add new files
1 parente0b5cda commit4d8602f

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
funcletterCombinations(digitsstring) []string {
4+
table:= []string{"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}
5+
ret:= []string{}
6+
iflen(digits)>0 {
7+
help(&ret,digits,"",0,table)
8+
}
9+
returnret
10+
}
11+
12+
funchelp(ret*[]string,digitsstring,curstring,indexint,table []string) {
13+
ifindex==len(digits) {
14+
*ret=append(*ret,cur)
15+
return
16+
}
17+
tmp:=table[digits[index]-48]
18+
for_,t:=rangetmp {
19+
help(ret,digits,cur+string(t),index+1,table)
20+
}
21+
}

‎46. Permutations.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
funcpermute(nums []int) [][]int {
4+
varret [][]int
5+
l:=len(nums)
6+
ifl==0 {
7+
returnret
8+
}
9+
helper(nums,0,l-1,&ret)
10+
returnret
11+
}
12+
13+
funchelper(nums []int,begin,endint,ret*[][]int) {
14+
ifbegin==end {
15+
t:=make([]int,len(nums))
16+
copy(t,nums)//这里一定要copy
17+
*ret=append(*ret,t)
18+
return
19+
}
20+
21+
fori:=begin;i<=end;i++ {
22+
nums[begin],nums[i]=nums[i],nums[begin]
23+
helper(nums,begin+1,end,ret)
24+
nums[begin],nums[i]=nums[i],nums[begin]
25+
}
26+
}

‎463. Island Perimeter.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
funcislandPerimeter(grid [][]int)int {
4+
row:=len(grid)
5+
col:=len(grid[0])
6+
count:=0
7+
fori:=0;i<row;i++ {
8+
forj:=0;j<col;j++ {
9+
ifgrid[i][j]==1 {
10+
t:=4
11+
ifj>=1&&grid[i][j-1]==1 {//左边有邻居
12+
t--
13+
}
14+
ifj<col-1&&grid[i][j+1]==1 {//右边有邻居
15+
t--
16+
}
17+
ifi>=1&&grid[i-1][j]==1 {//上面有邻居
18+
t--
19+
}
20+
ifi<row-1&&grid[i+1][j]==1 {//下面有邻居
21+
t--
22+
}
23+
count+=t
24+
}
25+
}
26+
}
27+
returncount
28+
}

‎75. Sort Colors.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
funcswap(nums []int,i,jint) {
4+
tmp:=nums[i]
5+
nums[i]=nums[j]
6+
nums[j]=tmp
7+
}
8+
funcsortColors(nums []int) {
9+
l,i,r:=0,0,len(nums)-1
10+
fori<=r {
11+
ifnums[i]==0 {
12+
swap(nums,i,l)
13+
i++
14+
l++
15+
}else {
16+
ifnums[i]==1 {
17+
i++
18+
}else {
19+
swap(nums,i,r)
20+
r--
21+
}
22+
23+
}
24+
}
25+
}

‎8. String to Integer (atoi).go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"math"
5+
"strings"
6+
)
7+
8+
funcmyAtoi(strstring)int {
9+
pos:=1
10+
res:=0
11+
str=strings.TrimSpace(str)
12+
iflen(str)==0 {
13+
returnres
14+
}
15+
i:=0
16+
ifstr[i]=='+' {
17+
i++
18+
pos=1
19+
}elseifstr[i]=='-' {
20+
i++
21+
pos=-1
22+
}
23+
for ;i<len(str);i++ {
24+
ifpos*res>=math.MaxInt32 {
25+
returnmath.MaxInt32
26+
}
27+
ifpos*res<=math.MinInt32 {
28+
returnmath.MinInt32
29+
}
30+
ifstr[i]<'0'||string(str[i])>"9" {
31+
returnres*pos
32+
}
33+
res=res*10+int(str[i])-'0'
34+
}
35+
ifpos*res>=math.MaxInt32 {
36+
returnmath.MaxInt32
37+
}
38+
ifpos*res<=math.MinInt32 {
39+
returnmath.MinInt32
40+
}
41+
returnpos*res
42+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp