Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions C++/004.Median of Two Sorted Arrays.cpp
Original file line number Diff line number Diff line change
@@ -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<int>& nums1, vector<int>& 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<len1&&j<len2&&k<mid_len){
if(nums1[i]<=nums2[j]){
store[k++] = nums1[i++];
}
else if(nums2[j]<=nums1[i]){
store[k++] = nums2[j++];
}
}
while(i<len1&&k<mid_len){
store[k++] = nums1[i++];

}
while(j<len2&&k<mid_len){
store[k++] = nums2[j++];
}
if((len1+len2)%2==1)
return store[mid_len-1];
else
return (store[mid_len-1]+store[mid_len-2]) /2;
}
};
11 changes: 9 additions & 2 deletions C++/008. String to Integer (atoi).cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
8. String to Integer (atoi)
8. String to Integer (atoi)
My Submissions QuestionEditorial Solution
Total Accepted: 99420 Total Submissions: 737262 Difficulty: Easy
Implement atoi to convert a string to an integer.
Expand Down Expand Up @@ -46,6 +46,13 @@ class Solution {
break;
}
}
//remove the '0' in front,wich is useless, to reduce the length
// the input “0000000002000” will not pass now if without the operation
int j=0;
while(str[j]=='0')
j++;
str = str.substr(j);

if(str.length() == 0)
return 0;
if(str.length() > 10) {
Expand All @@ -65,4 +72,4 @@ class Solution {
return (int)ans;
}
}
};
};