mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 16:36:41 +08:00
add problem 88
This commit is contained in:
29
Algorithms/88.Merge Sorted Array/Merge Sorted Array.go
Normal file
29
Algorithms/88.Merge Sorted Array/Merge Sorted Array.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package problem88
|
||||||
|
|
||||||
|
func merge(nums1 []int, m int, nums2 []int, n int) {
|
||||||
|
if m == 0 {
|
||||||
|
copy(nums1, nums2)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 这里不需要,因为测试数据考虑到了第一个数组的空间问题
|
||||||
|
// for index := 0; index < n; index++ {
|
||||||
|
// nums1 = append(nums1, nums2[index])
|
||||||
|
// }
|
||||||
|
i := m - 1
|
||||||
|
j := n - 1
|
||||||
|
k := m + n - 1
|
||||||
|
// 从后面往前放,只需要循环一次即可
|
||||||
|
for ; i >= 0 && j >= 0; k-- {
|
||||||
|
if nums1[i] > nums2[j] {
|
||||||
|
nums1[k] = nums1[i]
|
||||||
|
i--
|
||||||
|
} else {
|
||||||
|
nums1[k] = nums2[j]
|
||||||
|
j--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for ; j >= 0; k-- {
|
||||||
|
nums1[k] = nums2[j]
|
||||||
|
j--
|
||||||
|
}
|
||||||
|
}
|
59
Algorithms/88.Merge Sorted Array/Merge Sorted Array_test.go
Normal file
59
Algorithms/88.Merge Sorted Array/Merge Sorted Array_test.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package problem412
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type question struct {
|
||||||
|
para
|
||||||
|
ans
|
||||||
|
}
|
||||||
|
|
||||||
|
// para 是参数
|
||||||
|
// one 代表第一个参数
|
||||||
|
type para struct {
|
||||||
|
one []int
|
||||||
|
m int
|
||||||
|
two []int
|
||||||
|
n int
|
||||||
|
}
|
||||||
|
|
||||||
|
// ans 是答案
|
||||||
|
// one 代表第一个答案
|
||||||
|
type ans struct {
|
||||||
|
one []int
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Problem0088(t *testing.T) {
|
||||||
|
|
||||||
|
qs := []question{
|
||||||
|
|
||||||
|
// question{
|
||||||
|
// para{[]int{0}, 0, []int{1}, 1},
|
||||||
|
// ans{[]int{1}},
|
||||||
|
// },
|
||||||
|
//
|
||||||
|
// question{
|
||||||
|
// para{[]int{1, 3, 5, 7}, 4, []int{2, 4}, 2},
|
||||||
|
// ans{[]int{1, 2, 3, 4}},
|
||||||
|
// },
|
||||||
|
//
|
||||||
|
// question{
|
||||||
|
// para{[]int{1, 3, 5, 7}, 4, []int{2, 2}, 2},
|
||||||
|
// ans{[]int{1, 2, 2, 3}},
|
||||||
|
// },
|
||||||
|
|
||||||
|
question{
|
||||||
|
para{[]int{1, 2, 3, 0, 0, 0}, 3, []int{2, 5, 6}, 3},
|
||||||
|
ans{[]int{1, 2, 2, 3}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, q := range qs {
|
||||||
|
_, p := q.ans, q.para
|
||||||
|
|
||||||
|
merge(p.one, p.m, p.two, p.n)
|
||||||
|
fmt.Printf("~~%v~~\n", p)
|
||||||
|
}
|
||||||
|
}
|
8
Algorithms/88.Merge Sorted Array/README.md
Normal file
8
Algorithms/88.Merge Sorted Array/README.md
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# [88. Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/description/)
|
||||||
|
|
||||||
|
## 题目
|
||||||
|
|
||||||
|
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
|
||||||
|
|
||||||
|
Note:
|
||||||
|
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
|
Reference in New Issue
Block a user