Median of Two Sorted Arrays
xThere 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.
중간값 (Median)을 구하라는 문제이다.
홀수개일 때는 중간값을, 짝수개일때는 중간 값 2개의 평균을 반환하면 된다.
xxxxxxxxxxclass Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int[] arr = new int[nums1.length + nums2.length]; System.arraycopy(nums1, 0, arr, 0, nums1.length); System.arraycopy(nums2, 0, arr, nums1.length, nums2.length); Arrays.sort(arr); if (arr.length % 2 == 1) { return (double) arr[arr.length / 2]; } else { return ((double) arr[arr.length / 2] + (double)arr[arr.length / 2 - 1]) / 2.0; } }}'공부 > algorithm' 카테고리의 다른 글
| [codility] Fish (0) | 2019.12.12 |
|---|---|
| [codility] Brackets (0) | 2019.12.11 |
| [LeetCode] #3 Longest Substring Without Repeating Characters (0) | 2019.12.05 |
| [LeetCode] #1 Two Sum (0) | 2019.12.05 |
| [codewars] Which are in? (0) | 2019.03.29 |
댓글