“The Linux philosophy is “Laugh in the face of danger”.Oops.Wrong One. “Do it yourself”. Yes, that”s it.” Linux的哲学就是“在危险面前放声大笑”,呵呵,不是这句,应该是“一切靠自己,自力更生”才对。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
public class { public double findMedianSortedArrays (int [] nums1, int [] nums2) { int n = nums1.length, m = nums2.length; int res = 0 ; int median = (n + m+1 ) / 2 ; boolean flag = true ; if ((n + m) % 2 == 0 ) flag = false ; int i = 0 , j = 0 , count = 0 ; while (i < nums1.length && j < nums2.length) { if (nums1[i] < nums2[j]) { res = nums1[i]; ++i; } else { res = nums2[j]; ++j; } if (++count == median) { if (flag == false ) { if ( i<nums1.length && j<nums2.length) { res += nums1[i] < nums2[j] ? nums1[i] : nums2[j]; } else { res+=i<nums1.length?nums1[i]:nums2[j]; } double ret = (double ) res / 2 ; return ret; } return res; } } while (i<nums1.length){ res = nums1[i]; ++i; if (++count == median) { if (flag == false ) { res += nums1[i]; double ret = (double )res/2 ; return ret; } return res; } } while (j<nums2.length){ res = nums2[j]; ++j; if (++count == median) { if (flag == false ) { res += nums2[j]; double ret = (double )res/2 ; return ret; } return res; } } return -1 ; } public static void main (String []args) { int []nums1 = new int []{1 ,2 }; int nums2[] = new int []{3 ,4 }; System.out.println(new leetcode4().findMedianSortedArrays(nums1,nums2)); } }
近期评论