我无法在 Go 中使用 for 循环在幻灯片中分配值

发布于 2025-01-09 21:17:49 字数 1524 浏览 1 评论 0原文

你好,我正在尝试在 Leetcode 中学习数组。

我知道幻灯片值将通过使用 array[index] = value 反映在原始数组上。

但这段代码并没有按照我最初的想法工作。

我不能使用指针和返回,因为我只能在 Leetcode 中手动操作函数。

请您检查一下我的代码好吗?

我使用带分区的快速排序。

谢谢。

https://go.dev/play/p/dNgYqPZCZqK

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) //  Expected : [1 2 2 3 5 6] but [1 2 3 0 0 0]
}

func merge(nums1 []int, m int, nums2 []int, n int) {
    count := 0
    nums1 = append(nums1, nums2...)
    nums1 = quicksort(nums1, 0, m+n-1)
    for i := range nums1 {
        if nums1[i] == 0 {
            count++
        }
    }
    nums1 = nums1[count:]

    for j := range nums1 {
        nums1[j] = nums1[j] //self assignment for sure
    }
    fmt.Println(nums1)  // [1 2 2 3 5 6]

}
func quicksort(arr []int, low, high int) []int {
    if low < high {
        arr, p := partition(arr, low, high)
        arr = quicksort(arr, low, p-1)
        arr = quicksort(arr, p+1, high)
    }
    return arr
}

func partition(arr []int, low, high int) ([]int, int) {
    pivot := arr[high]
    i := low
    for j := low; j < high; j++ {
        if arr[j] < pivot {
            arr[i], arr[j] = arr[j], arr[i]
            i++
        }
    }
    arr[i], arr[high] = arr[high], arr[i]
    return arr, i
}

Hello I am trying to study array in Leetcode.

I know that slide value will be reflected on original array by using array[index] = value.

But this code didn't work what I thought originally.

I cannot use pointer and return because I can only hand function in Leetcode.

Would you kindly check below my code?

I used Quick sort with partition.

Thank you.

https://go.dev/play/p/dNgYqPZCZqK

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) //  Expected : [1 2 2 3 5 6] but [1 2 3 0 0 0]
}

func merge(nums1 []int, m int, nums2 []int, n int) {
    count := 0
    nums1 = append(nums1, nums2...)
    nums1 = quicksort(nums1, 0, m+n-1)
    for i := range nums1 {
        if nums1[i] == 0 {
            count++
        }
    }
    nums1 = nums1[count:]

    for j := range nums1 {
        nums1[j] = nums1[j] //self assignment for sure
    }
    fmt.Println(nums1)  // [1 2 2 3 5 6]

}
func quicksort(arr []int, low, high int) []int {
    if low < high {
        arr, p := partition(arr, low, high)
        arr = quicksort(arr, low, p-1)
        arr = quicksort(arr, p+1, high)
    }
    return arr
}

func partition(arr []int, low, high int) ([]int, int) {
    pivot := arr[high]
    i := low
    for j := low; j < high; j++ {
        if arr[j] < pivot {
            arr[i], arr[j] = arr[j], arr[i]
            i++
        }
    }
    arr[i], arr[high] = arr[high], arr[i]
    return arr, i
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭ゆ眷念 2025-01-16 21:17:49

我将附加变量从 nums1 更改为 temp。
也许这使得 nums1 成为复制幻灯片。

因此,将 nums1 更改为 temp 后,稍后将 temp 中的值分配给 nums1。

有用!

https://go.dev/play/p/fU-06ZBWiFA

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) //  Expected : [1 2 2 3 5 6] but [1 2 3 0 0 0]
}

func merge(nums1 []int, m int, nums2 []int, n int) {
    count := 0
    temp := append(nums1, nums2...)
    temp = quicksort(temp, 0, m+n-1)
    for i := range temp {
        if temp[i] == 0 {
            count++
        }
    }
    temp = temp[count:]

    for j := range nums1 {
        nums1[j] = temp[j] //self assignment for sure
    }
    fmt.Println(nums1)

}
func quicksort(arr []int, low, high int) []int {
    if low < high {
        arr, p := partition(arr, low, high)
        arr = quicksort(arr, low, p-1)
        arr = quicksort(arr, p+1, high)
    }
    return arr
}

func partition(arr []int, low, high int) ([]int, int) {
    pivot := arr[high]
    i := low
    for j := low; j < high; j++ {
        if arr[j] < pivot {
            arr[i], arr[j] = arr[j], arr[i]
            i++
        }
    }
    arr[i], arr[high] = arr[high], arr[i]
    return arr, i
}

I changed append variable from nums1 to temp.
Maybe that made nums1 as copy slide.

So after changing nums1 to temp, later allocate values from temp to nums1.

It works!

https://go.dev/play/p/fU-06ZBWiFA

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) //  Expected : [1 2 2 3 5 6] but [1 2 3 0 0 0]
}

func merge(nums1 []int, m int, nums2 []int, n int) {
    count := 0
    temp := append(nums1, nums2...)
    temp = quicksort(temp, 0, m+n-1)
    for i := range temp {
        if temp[i] == 0 {
            count++
        }
    }
    temp = temp[count:]

    for j := range nums1 {
        nums1[j] = temp[j] //self assignment for sure
    }
    fmt.Println(nums1)

}
func quicksort(arr []int, low, high int) []int {
    if low < high {
        arr, p := partition(arr, low, high)
        arr = quicksort(arr, low, p-1)
        arr = quicksort(arr, p+1, high)
    }
    return arr
}

func partition(arr []int, low, high int) ([]int, int) {
    pivot := arr[high]
    i := low
    for j := low; j < high; j++ {
        if arr[j] < pivot {
            arr[i], arr[j] = arr[j], arr[i]
            i++
        }
    }
    arr[i], arr[high] = arr[high], arr[i]
    return arr, i
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文