- class Solution {
- public double findMedianSortedArrays(int[] nums1, int[] nums2) {
- List<Integer> nums_a = new ArrayList<>();
- List<Integer> nums_b = new ArrayList<>(http://www.my516.com);
- for (int i = 0; i < nums1.length; i++) {
- nums_a.add(nums1[i]);
- }
- for (int i = 0; i < nums2.length; i++) {
- nums_b.add(nums2[i]);
- }
- nums_a.addAll(nums_b);
- Collections.sort(nums_a);
- if (nums_a.size() % 2 == 0) {
- int mid = nums_a.size() / 2;
- return (nums_a.get(mid) + nums_a.get(mid - 1)) / 2.0f;
- } else {
- int mid = (nums_a.size() - 1) / 2;
- return nums_a.get(mid);
- }
- }
- }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
第二次提交
关于这段代码, 看详细题解, 得多看几遍.
https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/4-xun-zhao-liang-ge-you-xu-shu-zu-de-zhong-wei-shu/
来源: http://www.bubuko.com/infodetail-3101219.html