美文网首页
Median of Two Sorted Arrays

Median of Two Sorted Arrays

作者: 博林木木 | 来源:发表于2016-12-20 12:42 被阅读0次
    func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
        var nums []int
        i1 := 0
        i2 := 0
        len1 := len(nums1)
        len2 := len(nums2)
        for{
            if i1>=len1 && i2<len2{
                nums = append(nums,nums2[i2])
                i2++
                continue
            }else if i2>=len2 && i1<len1{
                nums = append(nums,nums1[i1])
                i1++
                continue
            }else if i2>=len2 && i1>=len1{
                break
            }
            
            if nums1[i1]<nums2[i2]{
                nums = append(nums,nums1[i1])
                i1++
            }else{
                nums = append(nums,nums2[i2])
                i2++
            }
        }
        len := len(nums)
        if len%2 == 0{
            return float64(nums[len/2] + nums[len/2-1])/2
        }else{
            return float64(nums[len/2])
        }
    
    
    
    }
    

    相关文章

      网友评论

          本文标题:Median of Two Sorted Arrays

          本文链接:https://www.haomeiwen.com/subject/utgxvttx.html