Median of Two Sorted Arrays
2016-12-20 本文已影响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])
}
}