Generation

translateSun, 15 Jan 2023

func findMedianSortedArrays(nums1 []int, nums2 []int) float64 { nums1 = append(nums1, nums2...) sort.Ints(nums1) length := len(nums1) if length % 2 == 0 { return float64(nums1[length/2]+nums1[length/2-1])/2 } else { return float64(nums1[length/2]) } }

double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { vector<int> nums; merge(nums1.begin(),nums1.end(),nums2.begin(),nums2.end(),back_inserter(nums)); sort(nums.begin(),nums.end()); int length = nums.size(); int mid = length/2; if(length%2==1){ return nums[mid]; } else { return double(nums[mid]+nums[mid-1])/2.0; }

Questions about programming?Chat with your personal AI assistant