Skip to content

Commit 8805172

Browse files
author
zj972
committed
add 41.42.43.44.45. golang
1 parent 3fa8118 commit 8805172

File tree

13 files changed

+628
-2
lines changed

13 files changed

+628
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
|038|Count and Say|[JavaScript & Golang](https://github.com/zj972/leetcode/tree/master/code/038.%20Count%20and%20Say)|Easy|
4545
|039|Combination Sum|[Golang](https://github.com/zj972/leetcode/tree/master/code/039.%20Combination%20Sum)|Medium|
4646
|040|Combination Sum II|[Golang](https://github.com/zj972/leetcode/tree/master/code/040.%20Combination%20Sum%20II)|Medium|
47+
|041|First Missing Positive|[Golang](https://github.com/zj972/leetcode/tree/master/code/041.%20First%20Missing%20Positive)|Hard|
48+
|042|Trapping Rain Water|[Golang](https://github.com/zj972/leetcode/tree/master/code/042.%20Trapping%20Rain%20Water)|Hard|
49+
|043|Multiply Strings|[Golang](https://github.com/zj972/leetcode/tree/master/code/043.%20Multiply%20Strings)|Medium|
50+
|044|Wildcard Matching|[Golang](https://github.com/zj972/leetcode/tree/master/code/044.%20Wildcard%20Matching)|Hard|
51+
|045|Jump Game II|[Golang](https://github.com/zj972/leetcode/tree/master/code/045.%20Jump%20Game%20II)|Hard|
4752
|058|Length of Last Word|[JavaScript](https://github.com/zj972/leetcode/tree/master/code/058.%20Length%20of%20Last%20Word)|Easy|
4853
|062|Unique Paths|[JavaScript](https://github.com/zj972/leetcode/tree/master/code/062.%20Unique%20Paths)|Medium|
4954
|066|Plus One|[JavaScript](https://github.com/zj972/leetcode/tree/master/code/066.%20Plus%20One)|Easy|

code/002. Add Two Numbers/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func main() {
6262
}
6363
}
6464

65-
// wrong answer by 刘乾
65+
// wrong answer by Leo
6666
//func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
6767
// out := new(ListNode)
6868
// out.Val = 0

code/017. Letter Combinations of a Phone Number/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ A mapping of digit to letters (just like on the telephone buttons) is given belo
99
##### Example:
1010

1111
Input: "23"
12+
1213
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
13-
Note:
14+
15+
##### Note:
1416

1517
Although the above answer is in lexicographical order, your answer could be in any order you want.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# 41. First Missing Positive
2+
3+
Given an unsorted integer array, find the smallest missing positive integer.
4+
5+
##### Example 1:
6+
7+
Input: [1,2,0]
8+
9+
Output: 3
10+
11+
##### Example 2:
12+
13+
Input: [3,4,-1,1]
14+
15+
Output: 2
16+
17+
##### Example 3:
18+
19+
Input: [7,8,9,11,12]
20+
21+
Output: 1
22+
23+
##### Note:
24+
25+
Your algorithm should run in O(n) time and uses constant extra space.
26+
27+
给定一个未排序的整数数组,找出其中没有出现的最小的正整数。
28+
29+
##### 示例 1:
30+
31+
输入: [1,2,0]
32+
33+
输出: 3
34+
35+
##### 示例 2:
36+
37+
输入: [3,4,-1,1]
38+
39+
输出: 2
40+
41+
##### 示例 3:
42+
43+
输入: [7,8,9,11,12]
44+
45+
输出: 1
46+
47+
##### 说明:
48+
49+
你的算法的时间复杂度应为O(n),并且只能使用常数级别的空间。
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
)
7+
8+
//use sort
9+
//Runtime: 0 ms, faster than 100.00% of Go online submissions for First Missing Positive.
10+
//func firstMissingPositive(nums []int) int {
11+
// sort.Ints(nums)
12+
// var j = 0
13+
// for i := 0; i < len(nums); i++ {
14+
// if nums[i] < 1 {
15+
// continue
16+
// }
17+
// if j != nums[i] {
18+
// if j+1 == nums[i] {
19+
// j++
20+
// continue
21+
// }
22+
// return j + 1
23+
// }
24+
// }
25+
// return j + 1
26+
//}
27+
28+
//no use sort
29+
//Runtime: 0 ms, faster than 100.00% of Go online submissions for First Missing Positive.
30+
func firstMissingPositive(nums []int) int {
31+
for i := 0; i < len(nums); i++ {
32+
for nums[i] != i+1 && nums[i] > 0 && nums[i] <= len(nums) && nums[nums[i]-1] != nums[i] {
33+
nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i]
34+
}
35+
}
36+
for i := 0; i < len(nums); i++ {
37+
if nums[i] != i+1 {
38+
return i + 1
39+
}
40+
}
41+
return len(nums) + 1
42+
}
43+
44+
func main() {
45+
var data = []int{1, 2, 2, 3, 6, 7}
46+
fmt.Println(firstMissingPositive(data))
47+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 42. Trapping Rain Water
2+
3+
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
4+
5+
![img](http://www.leetcode.com/static/images/problemset/rainwatertrap.png)
6+
7+
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
8+
9+
##### Example:
10+
11+
Input: [0,1,0,2,1,0,1,3,2,1,2,1]
12+
13+
Output: 6
14+
15+
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
16+
17+
![img](http://www.leetcode.com/static/images/problemset/rainwatertrap.png)
18+
19+
上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 感谢 Marcos 贡献此图。
20+
21+
##### 示例:
22+
23+
输入: [0,1,0,2,1,0,1,3,2,1,2,1]
24+
25+
输出: 6

code/042. Trapping Rain Water/main.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
//Runtime: 4 ms, faster than 100.00% of Go online submissions for Trapping Rain Water.
8+
//func trap(height []int) int {
9+
// var sum int
10+
// var i int
11+
// for i < len(height) {
12+
// if height[i] == 0 {
13+
// i++
14+
// continue
15+
// }
16+
// h := height[i]
17+
// var j int = i + 1
18+
// var right int
19+
// var tmp int
20+
// var maxJ int = i + 1
21+
// for j < len(height) {
22+
// if height[j] < h {
23+
// if height[maxJ] < height[j] {
24+
// maxJ = j
25+
// }
26+
// tmp += height[j]
27+
// j++
28+
// continue
29+
// }
30+
// right = height[j]
31+
// break
32+
// }
33+
// if right == 0 {
34+
// if maxJ == i+1 {
35+
// i++
36+
// continue
37+
// }
38+
// j = maxJ
39+
// h = height[j]
40+
// tmp = 0
41+
// for k := i + 1; k < j; k++ {
42+
// tmp += height[k]
43+
// }
44+
// }
45+
// sum += (j-i-1)*h - tmp
46+
// i = j
47+
// }
48+
// return sum
49+
//}
50+
51+
//Runtime: 4 ms, faster than 100.00% of Go online submissions for Trapping Rain Water.
52+
func trap(height []int) int {
53+
leng := len(height)
54+
left := 0
55+
right := leng - 1
56+
sum := 0
57+
maxRight := 0
58+
maxLeft := 0
59+
for left <= right {
60+
if height[left] <= height[right] {
61+
if height[left] >= maxLeft {
62+
maxLeft = height[left]
63+
} else {
64+
sum += maxLeft - height[left]
65+
}
66+
left++
67+
} else {
68+
if height[right] >= maxRight {
69+
maxRight = height[right]
70+
} else {
71+
sum += maxRight - height[right]
72+
}
73+
right--
74+
}
75+
}
76+
return sum
77+
}
78+
79+
func main() {
80+
var data = []int{4, 2, 3}
81+
fmt.Println(trap(data))
82+
}

code/043. Multiply Strings/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# 43. Multiply Strings
2+
3+
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.
4+
5+
##### Example 1:
6+
7+
Input: num1 = "2", num2 = "3"
8+
9+
Output: "6"
10+
11+
##### Example 2:
12+
13+
Input: num1 = "123", num2 = "456"
14+
15+
Output: "56088"
16+
17+
##### Note:
18+
19+
1.The length of both num1 and num2 is < 110.
20+
21+
2.Both num1 and num2 contain only digits 0-9.
22+
23+
3.Both num1 and num2 do not contain any leading zero, except the number 0 itself.
24+
25+
4.You must not use any built-in BigInteger library or convert the inputs to integer directly.
26+
27+
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。
28+
29+
##### 示例 1:
30+
31+
输入: num1 = "2", num2 = "3"
32+
33+
输出: "6"
34+
35+
##### 示例 2:
36+
37+
输入: num1 = "123", num2 = "456"
38+
39+
输出: "56088"
40+
41+
##### 说明:
42+
43+
1.num1 和 num2 的长度小于110。
44+
45+
2.num1 和 num2 只包含数字 0-9。
46+
47+
3.num1 和 num2 均不以零开头,除非是数字 0 本身。
48+
49+
4.不能使用任何标准库的大数类型(比如 BigInteger)或直接将输入转换为整数来处理。

code/043. Multiply Strings/main.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
//Runtime: 0 ms, faster than 100.00% of Go online submissions for Multiply Strings.
8+
func multiply(num1 string, num2 string) string {
9+
if num1 == "0" || num2 == "0" {
10+
return "0"
11+
}
12+
13+
list := make([]uint8, len(num1)+len(num2))
14+
for l := 0; l < len(list); l++ {
15+
list[l] = 48
16+
}
17+
for i := len(num1) - 1; i >= 0; i-- {
18+
var up uint8
19+
for j := len(num2) - 1; j >= 0; j-- {
20+
tmp := (num1[i]-48)*(num2[j]-48) + (list[i+j+1] - 48) + up
21+
up = tmp / 10
22+
list[i+j+1] = tmp%10 + 48
23+
}
24+
head := i
25+
for up != 0 {
26+
tmp := list[head] - 48 + up
27+
up = tmp / 10
28+
list[head] = tmp%10 + 48
29+
head--
30+
}
31+
}
32+
if list[0] == 48 {
33+
list = list[1:]
34+
}
35+
return string(list)
36+
}
37+
38+
func main() {
39+
fmt.Println(multiply("123", "456"))
40+
}

0 commit comments

Comments
 (0)