This also works in VSCode and Go playground. I don't know also.
Maybe I need to refer another codes.
package main
import "fmt"
func main() {
nums1 := []int{1, 2, 3, 0, 0, 0}
nums2 := []int{2, 5, 6}
merge(nums1, len(nums1), nums2, len(nums2))
fmt.Println(nums1)
}
func merge(nums1 []int, m int, nums2 []int, n int) {
temp := make([]int, 0)
if m > n {
i := 0
j := 0
for i < m {
if j == n {
break
}
switch {
case nums1[i] == 0 && nums2[j] != 0:
temp = append(temp, nums2[j])
i++
j++
case nums1[i] == 0:
i++
case nums2[j] == 0 && nums1[i] != 0:
temp = append(temp, nums1[i])
i++
j++
case nums2[j] == 0:
j++
case nums1[i] <= nums2[j]:
temp = append(temp, nums1[i])
i++
case nums1[i] > nums2[j]:
temp = append(temp, nums2[j])
j++
}
}
} else {
i := 0
j := 0
for j < n {
if i == m {
break
}
switch {
case nums1[i] == 0 && nums2[j] != 0:
temp = append(temp, nums2[j])
i++
j++
case nums1[i] == 0:
i++
case nums2[j] == 0 && nums1[i] != 0:
temp = append(temp, nums1[i])
i++
j++
case nums2[j] == 0:
j++
case nums1[i] <= nums2[j]:
temp = append(temp, nums1[i])
i++
case nums1[i] > nums2[j]:
temp = append(temp, nums2[j])
j++
}
}
}
for k := range temp {
nums1[k] = temp[k]
}
}
Array101 Remove Duplicates from Sorted Array in GO (0) | 2022.03.01 |
---|---|
Leetcode Array101 Remove Element in GO (0) | 2022.02.27 |
Arrays 101 Code from Java to Go(Array Deletions) (0) | 2022.02.26 |
Array101 Merge Sorted Array in GO (0) | 2022.02.25 |
Quick sort with merge array in Go (0) | 2022.02.24 |
댓글 영역