class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
if (nums1.length == 0 && nums2.length == 0) return 0;
int[] mergeArr = new int[nums1.length + nums2.length];
for(int i=0; i<mergeArr.length; i++) {
if(i<nums1.length) {
mergeArr[i] = nums1[i];
continue;
}
mergeArr[i] = nums2[mergeArr.length-i-1];
}
Arrays.sort(mergeArr);
int index = mergeArr.length/2;
if(mergeArr.length % 2==0) {
return Double.valueOf(mergeArr[index-1] + mergeArr[index]) / 2;
} else {
return Double.valueOf(mergeArr[index]);
}
}
}
[사이트]
https://leetcode.com/problems/median-of-two-sorted-arrays/
[해설 블로그]
https://engkimbs.tistory.com/623
'알고리즘' 카테고리의 다른 글
[코딩인터뷰완전분석] 1.2_순열확인_풀이 (0) | 2021.08.24 |
---|---|
[leetcode] Height Checker (0) | 2021.08.24 |
[코딩인터뷰완전분석] 1.1_중복이 없는가_풀이 (0) | 2021.08.24 |
알고리즘 학습법 (0) | 2021.08.21 |
[LeetCode] Palindrome Number (0) | 2021.06.22 |