From f08ec2d5b331b32fcb416e8e07277437025fa328 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8A=89=E5=AD=90=E9=93=AD?= Date: Sun, 12 May 2019 13:55:01 +0800 Subject: [PATCH] fix the 008.string to interger and add 004 --- C++/004.Median of Two Sorted Arrays.cpp | 52 +++++++++++++++++++++++++ C++/008. String to Integer (atoi).cpp | 11 +++++- 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 C++/004.Median of Two Sorted Arrays.cpp diff --git a/C++/004.Median of Two Sorted Arrays.cpp b/C++/004.Median of Two Sorted Arrays.cpp new file mode 100644 index 0000000..c1979e7 --- /dev/null +++ b/C++/004.Median of Two Sorted Arrays.cpp @@ -0,0 +1,52 @@ +4. Median of Two Sorted Arrays +There are two sorted arrays nums1 and nums2 of size m and n respectively. + +Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). + +You may assume nums1 and nums2 cannot be both empty. + +Example 1: + +nums1 = [1, 3] +nums2 = [2] + +The median is 2.0 +Example 2: + +nums1 = [1, 2] +nums2 = [3, 4] + +The median is (2 + 3)/2 = 2.5 + + +class Solution { +public: + double findMedianSortedArrays(vector& nums1, vector& nums2) { + int len1 = nums1.size(); + int len2 = nums2.size(); + int mid_len = (int)(len1+len2) /2+1; + //create an array to store the values of half of nums1 and nums2. + float store[mid_len] = {0}; + int k =0; + int i=0,j=0; + while(i 10) { @@ -65,4 +72,4 @@ class Solution { return (int)ans; } } -}; \ No newline at end of file +};