Skip to content

Commit 55887e2

Browse files
author
zj972
committed
add 46.47.48.49.50. golang
1 parent e34c987 commit 55887e2

File tree

14 files changed

+677
-15
lines changed

14 files changed

+677
-15
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
|043|Multiply Strings|[Golang](https://github.com/zj972/leetcode/tree/master/code/043.%20Multiply%20Strings)|Medium|
5050
|044|Wildcard Matching|[Golang](https://github.com/zj972/leetcode/tree/master/code/044.%20Wildcard%20Matching)|Hard|
5151
|045|Jump Game II|[Golang](https://github.com/zj972/leetcode/tree/master/code/045.%20Jump%20Game%20II)|Hard|
52+
|046|Permutations|[Golang](https://github.com/zj972/leetcode/tree/master/code/046.%20Permutations)|Medium|
53+
|047|Permutations II|[Golang](https://github.com/zj972/leetcode/tree/master/code/047.%20Permutations%20II)|Medium|
54+
|048|Rotate Image|[Golang](https://github.com/zj972/leetcode/tree/master/code/048.%20Rotate%20Image)|Medium|
55+
|049|Group Anagrams|[Golang](https://github.com/zj972/leetcode/tree/master/code/049.%20Group%20Anagrams)|Medium|
5256
|058|Length of Last Word|[JavaScript](https://github.com/zj972/leetcode/tree/master/code/058.%20Length%20of%20Last%20Word)|Easy|
5357
|062|Unique Paths|[JavaScript](https://github.com/zj972/leetcode/tree/master/code/062.%20Unique%20Paths)|Medium|
5458
|066|Plus One|[JavaScript](https://github.com/zj972/leetcode/tree/master/code/066.%20Plus%20One)|Easy|

code/045. Jump Game II/main.go

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,43 @@ import (
3030
// return jumpFrom(0, nums, list)
3131
//}
3232

33+
//Runtime: 12 ms, faster than 100.00% of Go online submissions for Jump Game II.
34+
//func jump(nums []int) int {
35+
// n := len(nums)
36+
// if n == 0 || n == 1 || nums[0] == 0 {
37+
// return 0
38+
// }
39+
// start, end, jumps := 0, 0, 0
40+
// for dest := 0; dest < n-1; {
41+
// for i := start; i <= end; i++ {
42+
// if dest < i+nums[i] {
43+
// dest = i + nums[i]
44+
// }
45+
// }
46+
// start = end + 1
47+
// end = dest
48+
// jumps++
49+
// }
50+
// return jumps
51+
//}
52+
3353
//Runtime: 12 ms, faster than 100.00% of Go online submissions for Jump Game II.
3454
func jump(nums []int) int {
35-
n := len(nums)
36-
if n == 0 || n == 1 || nums[0] == 0 {
37-
return 0
38-
}
39-
start, end, jumps := 0, 0, 0
40-
for dest := 0; dest < n-1; {
41-
for i := start; i <= end; i++ {
42-
if dest < i+nums[i] {
43-
dest = i + nums[i]
44-
}
55+
var ret, curMax, curRch int
56+
for i := 0; i < len(nums); i++ {
57+
fmt.Println(i, nums[i], curMax, curRch, ret)
58+
if curRch < i {
59+
ret++
60+
curRch = curMax
61+
}
62+
if curMax < nums[i]+i {
63+
curMax = nums[i] + i
4564
}
46-
start = end + 1
47-
end = dest
48-
jumps++
4965
}
50-
return jumps
66+
return ret
5167
}
5268

5369
func main() {
54-
var data = []int{2, 3, 1, 1, 4}
70+
var data = []int{4, 5, 2, 1, 3, 4, 5, 1, 6, 2, 1}
5571
fmt.Println(jump(data))
5672
}

code/046. Permutations/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 46. Permutations
2+
3+
Given a collection of distinct integers, return all possible permutations.
4+
5+
##### Example:
6+
7+
Input: [1,2,3]
8+
9+
Output:
10+
11+
```text
12+
[
13+
[1,2,3],
14+
[1,3,2],
15+
[2,1,3],
16+
[2,3,1],
17+
[3,1,2],
18+
[3,2,1]
19+
]
20+
```
21+
22+
23+
给定一个没有重复数字的序列,返回其所有可能的全排列。
24+
25+
##### 示例:
26+
27+
输入: [1,2,3]
28+
29+
输出:
30+
31+
```text
32+
[
33+
[1,2,3],
34+
[1,3,2],
35+
[2,1,3],
36+
[2,3,1],
37+
[3,1,2],
38+
[3,2,1]
39+
]
40+
```

code/046. Permutations/main.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
//Runtime: 8 ms, faster than 83.59% of Go online submissions for Permutations.
8+
//func permute(nums []int) [][]int {
9+
// var add func(item []int, addList []int) [][]int
10+
// add = func(item []int, addList []int) [][]int {
11+
// if len(addList) == 0 {
12+
// return [][]int{item}
13+
// }
14+
// ret := [][]int{}
15+
// for i := 0; i < len(addList); i++ {
16+
// tmpItem := make([]int, len(item))
17+
// tmpAddList := make([]int, len(addList))
18+
// copy(tmpItem, item)
19+
// copy(tmpAddList, addList)
20+
// tmpItem = append(tmpItem, tmpAddList[i])
21+
// if i == len(addList)-1 {
22+
// tmpAddList = tmpAddList[:i]
23+
// } else {
24+
// tmpAddList = append(tmpAddList[:i], tmpAddList[i+1:]...)
25+
// }
26+
// tmp := add(tmpItem, tmpAddList)
27+
// ret = append(ret, tmp...)
28+
// }
29+
// return ret
30+
// }
31+
// return add([]int{}, nums)
32+
//}
33+
34+
//只使用原数组切片,没有tmp中间空间
35+
//Runtime: 4 ms, faster than 100.00% of Go online submissions for Permutations.
36+
func permute(nums []int) [][]int {
37+
var allPermutations [][]int
38+
var genPermutation func(nums []int, allPermutations *[][]int, index int)
39+
genPermutation = func(nums []int, allPermutations *[][]int, index int) {
40+
if index == len(nums) {
41+
*allPermutations = append(*allPermutations, append([]int{}, nums...))
42+
return
43+
}
44+
for i := index; i < len(nums); i++ {
45+
if i == index {
46+
genPermutation(nums, allPermutations, index+1)
47+
} else {
48+
nums[i], nums[index] = nums[index], nums[i]
49+
genPermutation(nums, allPermutations, index+1)
50+
nums[i], nums[index] = nums[index], nums[i]
51+
}
52+
}
53+
}
54+
genPermutation(nums, &allPermutations, 0)
55+
return allPermutations
56+
}
57+
58+
func main() {
59+
var data = []int{4, 5, 2, 1}
60+
fmt.Println(permute(data))
61+
}

code/047. Permutations II/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 47. Permutations II
2+
3+
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
4+
5+
##### Example:
6+
7+
Input: [1,1,2]
8+
9+
Output:
10+
11+
```text
12+
[
13+
[1,1,2],
14+
[1,2,1],
15+
[2,1,1]
16+
]
17+
```
18+
19+
20+
给定一个可包含重复数字的序列,返回所有不重复的全排列。
21+
22+
##### 示例:
23+
24+
输入: [1,1,2]
25+
26+
输出:
27+
28+
```text
29+
[
30+
[1,1,2],
31+
[1,2,1],
32+
[2,1,1]
33+
]
34+
```

code/047. Permutations II/main.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
//在一的基础上过滤相同值的递归
8+
//Runtime: 12 ms, faster than 100.00% of Go online submissions for Permutations II.
9+
//func permuteUnique(nums []int) [][]int {
10+
// sort.Ints(nums)
11+
// var add func(item []int, addList []int) [][]int
12+
// add = func(item []int, addList []int) [][]int {
13+
// if len(addList) == 0 {
14+
// return [][]int{item}
15+
// }
16+
// ret := [][]int{}
17+
// for i := 0; i < len(addList); i++ {
18+
// if i != 0 && addList[i] == addList[i-1] {
19+
// continue
20+
// }
21+
// tmpItem := make([]int, len(item))
22+
// tmpAddList := make([]int, len(addList))
23+
// copy(tmpItem, item)
24+
// copy(tmpAddList, addList)
25+
// tmpItem = append(tmpItem, tmpAddList[i])
26+
// if i == len(addList)-1 {
27+
// tmpAddList = tmpAddList[:i]
28+
// } else {
29+
// tmpAddList = append(tmpAddList[:i], tmpAddList[i+1:]...)
30+
// }
31+
// tmp := add(tmpItem, tmpAddList)
32+
// ret = append(ret, tmp...)
33+
// }
34+
// return ret
35+
// }
36+
// return add([]int{}, nums)
37+
//}
38+
39+
//由于在基础数组进行交换操作,所以无法保证有序,使用map确认是否已经有相同值的递归
40+
//Runtime: 12 ms, faster than 100.00% of Go online submissions for Permutations II.
41+
func permuteUnique(nums []int) [][]int {
42+
var allPermutations [][]int
43+
var genPermutation func(nums []int, allPermutations *[][]int, index int)
44+
genPermutation = func(nums []int, allPermutations *[][]int, index int) {
45+
if index == len(nums) {
46+
*allPermutations = append(*allPermutations, append([]int{}, nums...))
47+
return
48+
}
49+
tmp := map[int]bool{}
50+
for i := index; i < len(nums); i++ {
51+
if i == index {
52+
genPermutation(nums, allPermutations, index+1)
53+
} else {
54+
if tmp[nums[i]] {
55+
continue
56+
}
57+
nums[i], nums[index] = nums[index], nums[i]
58+
genPermutation(nums, allPermutations, index+1)
59+
nums[i], nums[index] = nums[index], nums[i]
60+
}
61+
tmp[nums[i]] = true
62+
}
63+
}
64+
genPermutation(nums, &allPermutations, 0)
65+
return allPermutations
66+
}
67+
68+
func main() {
69+
var data = []int{0, 1, 0, 0, 9}
70+
fmt.Println(permuteUnique(data))
71+
}

code/048. Rotate Image/README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# 48. Rotate Image
2+
3+
You are given an n x n 2D matrix representing an image.
4+
5+
Rotate the image by 90 degrees (clockwise).
6+
7+
##### Note:
8+
9+
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
10+
11+
##### Example 1:
12+
13+
Given input matrix =
14+
15+
```text
16+
[
17+
[1,2,3],
18+
[4,5,6],
19+
[7,8,9]
20+
],
21+
```
22+
23+
rotate the input matrix in-place such that it becomes:
24+
25+
```text
26+
[
27+
[7,4,1],
28+
[8,5,2],
29+
[9,6,3]
30+
]
31+
```
32+
33+
##### Example 2:
34+
35+
Given input matrix =
36+
37+
```text
38+
[
39+
[ 5, 1, 9,11],
40+
[ 2, 4, 8,10],
41+
[13, 3, 6, 7],
42+
[15,14,12,16]
43+
],
44+
```
45+
46+
rotate the input matrix in-place such that it becomes:
47+
48+
```text
49+
[
50+
[15,13, 2, 5],
51+
[14, 3, 4, 1],
52+
[12, 6, 8, 9],
53+
[16, 7,10,11]
54+
]
55+
```
56+
57+
给定一个 n × n 的二维矩阵表示一个图像。
58+
59+
将图像顺时针旋转 90 度。
60+
61+
##### 说明:
62+
63+
你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。
64+
65+
##### 示例 1:
66+
67+
给定 matrix =
68+
69+
```text
70+
[
71+
[1,2,3],
72+
[4,5,6],
73+
[7,8,9]
74+
],
75+
```
76+
77+
原地旋转输入矩阵,使其变为:
78+
79+
```text
80+
[
81+
[7,4,1],
82+
[8,5,2],
83+
[9,6,3]
84+
]
85+
```
86+
87+
##### 示例 2:
88+
89+
给定 matrix =
90+
91+
```text
92+
[
93+
[ 5, 1, 9,11],
94+
[ 2, 4, 8,10],
95+
[13, 3, 6, 7],
96+
[15,14,12,16]
97+
],
98+
```
99+
100+
原地旋转输入矩阵,使其变为:
101+
102+
```text
103+
[
104+
[15,13, 2, 5],
105+
[14, 3, 4, 1],
106+
[12, 6, 8, 9],
107+
[16, 7,10,11]
108+
]
109+
```

0 commit comments

Comments
 (0)