题解 | #在两个长度相等的排序数组中找到上中位数#
在两个长度相等的排序数组中找到上中位数
http://www.nowcoder.com/practice/6fbe70f3a51d44fa9395cfc49694404f
class Solution {
public:
/**
* find median in two sorted array
* @param arr1 int整型vector the array1
* @param arr2 int整型vector the array2
* @return int整型
*/
int findMedianinTwoSortedAray(vector<int>& arr1, vector<int>& arr2) {
// write code here
int n=arr1.size();
int cur1=0,cur2=0;
for(int i=1;i<n;i++){
if(arr1[cur1]<arr2[cur2]){
cur1++;
}
else cur2++;
}
if(arr1[cur1]<arr2[cur2])
return arr1[cur1];
else return arr2[cur2];
}
};
public:
/**
* find median in two sorted array
* @param arr1 int整型vector the array1
* @param arr2 int整型vector the array2
* @return int整型
*/
int findMedianinTwoSortedAray(vector<int>& arr1, vector<int>& arr2) {
// write code here
int n=arr1.size();
int cur1=0,cur2=0;
for(int i=1;i<n;i++){
if(arr1[cur1]<arr2[cur2]){
cur1++;
}
else cur2++;
}
if(arr1[cur1]<arr2[cur2])
return arr1[cur1];
else return arr2[cur2];
}
};