4 번째 문제입니다.
난이도는 상...
역시 만만치 않네요..
힘들게 Accpet 했습니다.
저는 아래와 같이 풀었습니다.
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int nums1Size = nums1.size();
int nums2Size = nums2.size();
vector<int> composeVector;
int num1 = 0;
int num2 = 0;
int num1Pos = 0;
int num2Pos = 0;
while(nums1Size != num1Pos || nums2Size != num2Pos){
num2 = num1 = INT_MAX;
if(nums1Size != num1Pos)
num1 = nums1[num1Pos];
if(nums2Size != num2Pos)
num2 = nums2[num2Pos];
if(num1 < num2){
composeVector.push_back(num1);
num1Pos ++;
}else if(nums2Size != num2Pos){
composeVector.push_back(num2);
num2Pos ++;
}
}
int composeVectorSize = composeVector.size();
int pos = composeVectorSize / 2;
if(composeVectorSize % 2)
return composeVector[pos];
return (composeVector[pos - 1] + composeVector[pos]) / 2.0;
}
};
속도는 무려 98%~
'Dev Language > LeetCode' 카테고리의 다른 글
[LeetCode] 6. ZigZag Conversion (0) | 2018.01.10 |
---|---|
[LeetCode] 5. Longest Palindromic Substring (0) | 2018.01.10 |
[LeetCode] 3. Longest Substring Without Repeating Characters (0) | 2018.01.10 |
[LeetCode] 2. Add Two Number (0) | 2017.12.31 |
[LeetCode] 1. Two Sum (0) | 2017.12.31 |