Skip to content

Commit 8bb84f2

Browse files
committed
Add solution 43、45、73、227
1 parent a841eac commit 8bb84f2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1421
-354
lines changed

README.md

+261-260
Large diffs are not rendered by default.

leetcode/0008.String-to-Integer-atoi/8. String to Integer (atoi)_test.go

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question8 struct {
9+
para8
10+
ans8
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para8 struct {
16+
one string
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans8 struct {
22+
one int
23+
}
24+
25+
func Test_Problem8(t *testing.T) {
26+
27+
qs := []question8{
28+
29+
{
30+
para8{"42"},
31+
ans8{42},
32+
},
33+
34+
{
35+
para8{" -42"},
36+
ans8{-42},
37+
},
38+
39+
{
40+
para8{"4193 with words"},
41+
ans8{4193},
42+
},
43+
44+
{
45+
para8{"words and 987"},
46+
ans8{0},
47+
},
48+
49+
{
50+
para8{"-91283472332"},
51+
ans8{-2147483648},
52+
},
53+
}
54+
55+
fmt.Printf("------------------------Leetcode Problem 8------------------------\n")
56+
57+
for _, q := range qs {
58+
_, p := q.ans8, q.para8
59+
fmt.Printf("【input】:%v 【output】:%v\n", p.one, myAtoi(p.one))
60+
}
61+
fmt.Printf("\n\n\n")
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package leetcode
2+
3+
func multiply(num1 string, num2 string) string {
4+
if num1 == "0" || num2 == "0" {
5+
return "0"
6+
}
7+
b1, b2, tmp := []byte(num1), []byte(num2), make([]int, len(num1)+len(num2))
8+
for i := 0; i < len(b1); i++ {
9+
for j := 0; j < len(b2); j++ {
10+
tmp[i+j+1] += int(b1[i]-'0') * int(b2[j]-'0')
11+
}
12+
}
13+
for i := len(tmp) - 1; i > 0; i-- {
14+
tmp[i-1] += tmp[i] / 10
15+
tmp[i] = tmp[i] % 10
16+
}
17+
if tmp[0] == 0 {
18+
tmp = tmp[1:]
19+
}
20+
res := make([]byte, len(tmp))
21+
for i := 0; i < len(tmp); i++ {
22+
res[i] = '0' + byte(tmp[i])
23+
}
24+
return string(res)
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question43 struct {
9+
para43
10+
ans43
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para43 struct {
16+
num1 string
17+
num2 string
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans43 struct {
23+
one string
24+
}
25+
26+
func Test_Problem43(t *testing.T) {
27+
28+
qs := []question43{
29+
30+
{
31+
para43{"2", "3"},
32+
ans43{"6"},
33+
},
34+
35+
{
36+
para43{"123", "456"},
37+
ans43{"56088"},
38+
},
39+
}
40+
41+
fmt.Printf("------------------------Leetcode Problem 43------------------------\n")
42+
43+
for _, q := range qs {
44+
_, p := q.ans43, q.para43
45+
fmt.Printf("【input】:%v 【output】:%v\n", p, multiply(p.num1, p.num2))
46+
}
47+
fmt.Printf("\n\n\n")
48+
}
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# [43. Multiply Strings](https://leetcode.com/problems/multiply-strings/)
2+
3+
4+
## 题目
5+
6+
Given two non-negative integers `num1` and `num2` represented as strings, return the product of `num1` and `num2`, also represented as a string.
7+
8+
**Note:** You must not use any built-in BigInteger library or convert the inputs to integer directly.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: num1 = "2", num2 = "3"
14+
Output: "6"
15+
```
16+
17+
**Example 2:**
18+
19+
```
20+
Input: num1 = "123", num2 = "456"
21+
Output: "56088"
22+
```
23+
24+
**Constraints:**
25+
26+
- `1 <= num1.length, num2.length <= 200`
27+
- `num1` and `num2` consist of digits only.
28+
- Both `num1` and `num2` do not contain any leading zero, except the number `0` itself.
29+
30+
## 题目大意
31+
32+
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。
33+
34+
## 解题思路
35+
36+
- 用数组模拟乘法。创建一个数组长度为 `len(num1) + len(num2)` 的数组用于存储乘积。对于任意 `0 ≤ i < len(num1)``0 ≤ j < len(num2)``num1[i] * num2[j]` 的结果位于 `tmp[i+j+1]`,如果 `tmp[i+j+1]≥10`,则将进位部分加到 `tmp[i+j]`。最后,将数组 `tmp` 转成字符串,如果最高位是 0 则舍弃最高位。
37+
38+
## 代码
39+
40+
```go
41+
package leetcode
42+
43+
func multiply(num1 string, num2 string) string {
44+
if num1 == "0" || num2 == "0" {
45+
return "0"
46+
}
47+
b1, b2, tmp := []byte(num1), []byte(num2), make([]int, len(num1)+len(num2))
48+
for i := 0; i < len(b1); i++ {
49+
for j := 0; j < len(b2); j++ {
50+
tmp[i+j+1] += int(b1[i]-'0') * int(b2[j]-'0')
51+
}
52+
}
53+
for i := len(tmp) - 1; i > 0; i-- {
54+
tmp[i-1] += tmp[i] / 10
55+
tmp[i] = tmp[i] % 10
56+
}
57+
if tmp[0] == 0 {
58+
tmp = tmp[1:]
59+
}
60+
res := make([]byte, len(tmp))
61+
for i := 0; i < len(tmp); i++ {
62+
res[i] = '0' + byte(tmp[i])
63+
}
64+
return string(res)
65+
}
66+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package leetcode
2+
3+
func jump(nums []int) int {
4+
if len(nums) == 1 {
5+
return 0
6+
}
7+
needChoose, canReach, step := 0, 0, 0
8+
for i, x := range nums {
9+
if i+x > canReach {
10+
canReach = i + x
11+
if canReach >= len(nums)-1 {
12+
return step + 1
13+
}
14+
}
15+
if i == needChoose {
16+
needChoose = canReach
17+
step++
18+
}
19+
}
20+
return step
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question45 struct {
9+
para45
10+
ans45
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para45 struct {
16+
nums []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans45 struct {
22+
one int
23+
}
24+
25+
func Test_Problem45(t *testing.T) {
26+
27+
qs := []question45{
28+
29+
{
30+
para45{[]int{2, 3, 1, 1, 4}},
31+
ans45{2},
32+
},
33+
34+
{
35+
para45{[]int{2, 3, 0, 1, 4}},
36+
ans45{2},
37+
},
38+
}
39+
40+
fmt.Printf("------------------------Leetcode Problem 45------------------------\n")
41+
42+
for _, q := range qs {
43+
_, p := q.ans45, q.para45
44+
fmt.Printf("【input】:%v 【output】:%v\n", p, jump(p.nums))
45+
}
46+
fmt.Printf("\n\n\n")
47+
}

leetcode/0045.Jump-Game-II/README.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# [45. Jump Game II](https://leetcode.com/problems/jump-game-ii/)
2+
3+
4+
## 题目
5+
6+
Given an array of non-negative integers `nums`, you are initially positioned at the first index of the array.
7+
8+
Each element in the array represents your maximum jump length at that position.
9+
10+
Your goal is to reach the last index in the minimum number of jumps.
11+
12+
You can assume that you can always reach the last index.
13+
14+
**Example 1:**
15+
16+
```
17+
Input: nums = [2,3,1,1,4]
18+
Output: 2
19+
Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: nums = [2,3,0,1,4]
26+
Output: 2
27+
```
28+
29+
**Constraints:**
30+
31+
- `1 <= nums.length <= 1000`
32+
- `0 <= nums[i] <= 10^5`
33+
34+
## 题目大意
35+
36+
给定一个非负整数数组,你最初位于数组的第一个位置。数组中的每个元素代表你在该位置可以跳跃的最大长度。你的目标是使用最少的跳跃次数到达数组的最后一个位置。
37+
38+
## 解题思路
39+
40+
- 要求找到最少跳跃次数,顺理成章的会想到用贪心算法解题。扫描步数数组,维护当前能够到达最大下标的位置,记为能到达的最远边界,如果扫描过程中到达了最远边界,更新边界并将跳跃次数 + 1。
41+
- 扫描数组的时候,其实不需要扫描最后一个元素,因为在跳到最后一个元素之前,能到达的最远边界一定大于等于最后一个元素的位置,不然就跳不到最后一个元素,到达不了终点了;如果遍历到最后一个元素,说明边界正好为最后一个位置,最终跳跃次数直接 + 1 即可,也不需要访问最后一个元素。
42+
43+
## 代码
44+
45+
```go
46+
package leetcode
47+
48+
func jump(nums []int) int {
49+
if len(nums) == 1 {
50+
return 0
51+
}
52+
needChoose, canReach, step := 0, 0, 0
53+
for i, x := range nums {
54+
if i+x > canReach {
55+
canReach = i + x
56+
if canReach >= len(nums)-1 {
57+
return step + 1
58+
}
59+
}
60+
if i == needChoose {
61+
needChoose = canReach
62+
step++
63+
}
64+
}
65+
return step
66+
}
67+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package leetcode
2+
3+
func setZeroes(matrix [][]int) {
4+
if len(matrix) == 0 || len(matrix[0]) == 0 {
5+
return
6+
}
7+
isFirstRowExistZero, isFirstColExistZero := false, false
8+
for i := 0; i < len(matrix); i++ {
9+
if matrix[i][0] == 0 {
10+
isFirstColExistZero = true
11+
break
12+
}
13+
}
14+
for j := 0; j < len(matrix[0]); j++ {
15+
if matrix[0][j] == 0 {
16+
isFirstRowExistZero = true
17+
break
18+
}
19+
}
20+
for i := 1; i < len(matrix); i++ {
21+
for j := 1; j < len(matrix[0]); j++ {
22+
if matrix[i][j] == 0 {
23+
matrix[i][0] = 0
24+
matrix[0][j] = 0
25+
}
26+
}
27+
}
28+
// 处理[1:]行全部置 0
29+
for i := 1; i < len(matrix); i++ {
30+
if matrix[i][0] == 0 {
31+
for j := 1; j < len(matrix[0]); j++ {
32+
matrix[i][j] = 0
33+
}
34+
}
35+
}
36+
// 处理[1:]列全部置 0
37+
for j := 1; j < len(matrix[0]); j++ {
38+
if matrix[0][j] == 0 {
39+
for i := 1; i < len(matrix); i++ {
40+
matrix[i][j] = 0
41+
}
42+
}
43+
}
44+
if isFirstRowExistZero {
45+
for j := 0; j < len(matrix[0]); j++ {
46+
matrix[0][j] = 0
47+
}
48+
}
49+
if isFirstColExistZero {
50+
for i := 0; i < len(matrix); i++ {
51+
matrix[i][0] = 0
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)