Skip to content

Commit 4d8602f

Browse files
committed
add new files
1 parent e0b5cda commit 4d8602f

5 files changed

+142
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
func letterCombinations(digits string) []string {
4+
table := []string{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}
5+
ret := []string{}
6+
if len(digits) > 0 {
7+
help(&ret, digits, "", 0, table)
8+
}
9+
return ret
10+
}
11+
12+
func help(ret *[]string, digits string, cur string, index int, table []string) {
13+
if index == len(digits) {
14+
*ret = append(*ret, cur)
15+
return
16+
}
17+
tmp := table[digits[index]-48]
18+
for _, t := range tmp {
19+
help(ret, digits, cur+string(t), index+1, table)
20+
}
21+
}

46. Permutations.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
func permute(nums []int) [][]int {
4+
var ret [][]int
5+
l := len(nums)
6+
if l == 0 {
7+
return ret
8+
}
9+
helper(nums, 0, l-1, &ret)
10+
return ret
11+
}
12+
13+
func helper(nums []int, begin, end int, ret *[][]int) {
14+
if begin == end {
15+
t := make([]int, len(nums))
16+
copy(t, nums) //这里一定要copy
17+
*ret = append(*ret, t)
18+
return
19+
}
20+
21+
for i := 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

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

75. Sort Colors.go

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

8. String to Integer (atoi).go

+42
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+
func myAtoi(str string) int {
9+
pos := 1
10+
res := 0
11+
str = strings.TrimSpace(str)
12+
if len(str) == 0 {
13+
return res
14+
}
15+
i := 0
16+
if str[i] == '+' {
17+
i++
18+
pos = 1
19+
} else if str[i] == '-' {
20+
i++
21+
pos = -1
22+
}
23+
for ; i < len(str); i++ {
24+
if pos*res >= math.MaxInt32 {
25+
return math.MaxInt32
26+
}
27+
if pos*res <= math.MinInt32 {
28+
return math.MinInt32
29+
}
30+
if str[i] < '0' || string(str[i]) > "9" {
31+
return res * pos
32+
}
33+
res = res*10 + int(str[i]) - '0'
34+
}
35+
if pos*res >= math.MaxInt32 {
36+
return math.MaxInt32
37+
}
38+
if pos*res <= math.MinInt32 {
39+
return math.MinInt32
40+
}
41+
return pos * res
42+
}

0 commit comments

Comments
 (0)