what's wrong with my golang solution


  • 0
    T

    maybe i shold import "sort",i just write a quick sort for practice.
    this code works fine on my computer however fail to submit

    func Qsort(num []int, left int, right int) {
        if left == right {
             return
        }   
        tmp := num[left]
        i, j := left, right
        for i < j { 
            for {
                if num[j] > tmp && j > i { 
                    j-- 
                } else {
                    break
                }   
            }   
            if j > i { 
                num[i] = num[j]
                i++ 
            }   
            for {
                if num[i] < tmp && i < j { 
                    i++ 
                } else {
                    break
                }   
            }   
            if j > i { 
                num[j] = num[i]
                j-- 
            }   
        }   
        num[i] = tmp 
        if left < i { 
            Qsort(num, left, i-1)
        }   
        if right > i { 
            Qsort(num, i+1, right)
        }   
    }
    
    func intersect(nums1 []int, nums2 []int) []int {
        Qsort(nums1,0,len(nums1))
        Qsort(nums2,0,len(nums2))
        i, j := 0,0
        res := make([]int,0)
        for i<len(nums1) && j<len(nums2) {
            if nums1[i] == nums2[j] {
                res = append(res,nums1[i])
                i++
                j++
            } else {
                if nums1[i] < nums2[j] {
                    i++
                } else {
                    j++
                }
            }
        }
        return res
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.