Leetcode Median of Two Sorted Arrays (java),leetcodemedian

來源:互聯網
上載者:User

Leetcode Median of Two Sorted Arrays (java),leetcodemedian

解法一:

import java.util.Arrays;class Solution {    public double findMedianSortedArrays(int[] nums1, int[] nums2) {        //儲存合并後的數組        int[] nums3 = new int[nums1.length + nums2.length];        for (int i = 0; i < nums3.length; i++) {             if(i < nums1.length){                 nums3[i] = nums1[i];             }             else {                 nums3[i] = nums2[i-nums1.length];             }        }        //進行升序排序        Arrays.sort(nums3);        //尋找median        double median;        if(nums3.length % 2 == 1){             median = nums3[(nums3.length/2) ];        }        else{             median = (nums3[((nums3.length/2))]+nums3[(nums3.length/2)-1])/2.0;        }        return median;    }}

該解法思路是把兩個數組合并,進行升序排序,取中間值

 

解法二:

class Solution {    public double findMedianSortedArrays(int[] nums1, int[] nums2) {       int n = nums1.length;       int m = nums2.length;       //讓nums1始終最短       //若num2比nums1長會出現數組越界問題       if(n > m){           return findMedianSortedArrays(nums2,nums1);       }       int l1;//nums1左部分最右的元素       int r1;//nums1右部分最左的元素       int c1;//nums1分割位置       int l2;//nums2左部分最右的元素       int r2;//nums2右部分最左的元素       int c2;//nums2分割部分        //讓數組恒為奇數       int start = 0;       int end = 2 * n + 1 - 1;       //開始不斷分割nums1       while (start <= end){           //c1 c2保證了是前m+n個數 數組總長為2(m+n)           c1 = (start + end) / 2;           c2 = m + n - c1;           l1 = c1 == 0 ? Integer.MIN_VALUE : nums1[(c1 - 1 ) / 2];           r1 = c1 >= 2*n ? Integer.MAX_VALUE : nums1[(c1) / 2];           l2 = c2 == 0 ? Integer.MIN_VALUE : nums2[(c2 - 1) / 2];           r2 = c2 >= 2*m ? Integer.MAX_VALUE : nums2[(c2) / 2];           //l1減少 r2增大           if(l1 > r2){               end = c1 - 1;           }           //r1增大 l2減少           else if(l2 > r1){               start = c1 + 1;           }           else {               double median = ( Math.max(l1,l2) + Math.min(r1,r2) ) / 2.0;               return median;           }       }       return 0.0;    }}

 該解法思路是:

  用兩個分割位置把兩個數組各自分成兩部分,兩個數組即是四部分,為l1,r1,l2,r2

  由於取的是中值,即是取第m+n個(這個做法是因為把數組長度都假設成了2*n + 1個來讓數組為奇數個)

  分割的位置先是第一個數組的一半,第二個數組是 m+n - 第一個數組的位置,為了保證兩個數組的左部分加起來個數是m+n來取到第m+n個數

  並且要保證兩個左半部分要恒小於兩個右半部分,即是l1<r1&&l1<r2 和 l2<r2&&l2<r1 因為已經是有序數組,所以有一半可以忽略,即是 l1<r2 && l2<r1

  若不滿足上述條件,就把分割位置進行移動,此時正確的位置肯定在其中一邊,把另一邊捨去後繼續二分直到符合上述條件

  當滿足上述條件,l1和l2最大的,r1和r2最小的,它們的和除以2就可以得出答案

關於邊界問題:

  只要保證nums1<=nums2即可,因為若出現nums1>nums2的時候,會出現nums2的分割位置出現數組越界的情況

 

github地址:https://github.com/CyanChan/leetcode

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.