LeetCode算法(4)_尋找兩個正序數組的中位數

LeetCode算法(4)_尋找兩個正序數組的中位數

import java.util.Arrays;
/*
 * @lc app=leetcode.cn id=4 lang=java
 *
 * [4] 尋找兩個正序數組的中位數
 */

// @lc code=start
class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
         int [] list = new int[nums1.length+nums2.length];
         System.arraycopy(nums1, 0, list, 0, nums1.length);
         System.arraycopy(nums2, 0, list, nums1.length, nums2.length);
         Arrays.sort(list);
        
         
         int sub = list.length/2;
         return (list.length%2!=0)?list[sub]:
            (list[sub-1]+list[sub])/2.0f;   
    }
}
// @lc code=end

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章