diff --git a/0004-median-of-two-sorted-arrays/Code/1.java b/0004-median-of-two-sorted-arrays/Code/1.java index cfb36cfd..1ca634e4 100644 --- a/0004-median-of-two-sorted-arrays/Code/1.java +++ b/0004-median-of-two-sorted-arrays/Code/1.java @@ -1,6 +1,6 @@ public class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { - // ʹnums1��Ϊ�϶�����,����������߼����ٶ�,ͬʱ���Ա���һЩ�߽����� + // Ensure nums1 is the smaller array if (nums1.length > nums2.length) { int[] temp = nums1; nums1 = nums2; @@ -9,46 +9,39 @@ public double findMedianSortedArrays(int[] nums1, int[] nums2) { int len1 = nums1.length; int len2 = nums2.length; - int leftLen = (len1 + len2 + 1) / 2; //������ϲ�&�����,���ߵij��� + int leftLen = (len1 + len2 + 1) / 2; // Half of the total length (rounded up) - // ������1���ж��ּ��� int start = 0; int end = len1; while (start <= end) { - // ��������ı�����A,B��λ��(��1��ʼ����) - // count1 = 2 ��ʾ num1 ����ĵ�2������ - // ��index��1 - int count1 = start + ((end - start) / 2); + // Partition indices in nums1 and nums2 + int count1 = start + (end - start) / 2; int count2 = leftLen - count1; if (count1 > 0 && nums1[count1 - 1] > nums2[count2]) { - // A��B��next��Ҫ�� + // Move partition in nums1 to the left end = count1 - 1; } else if (count1 < len1 && nums2[count2 - 1] > nums1[count1]) { - // B��A��next��Ҫ�� + // Move partition in nums1 to the right start = count1 + 1; } else { - // ��ȡ��λ�� - int result = (count1 == 0)? nums2[count2 - 1]: // ��num1������������������ұ� - (count2 == 0)? nums1[count1 - 1]: // ��num2������������������ұ� - Math.max(nums1[count1 - 1], nums2[count2 - 1]); // �Ƚ�A,B - if (isOdd(len1 + len2)) { - return result; + // We have found the correct partition + int result = (count1 == 0) ? nums2[count2 - 1] : // If count1 is 0, take from nums2 + (count2 == 0) ? nums1[count1 - 1] : // If count2 is 0, take from nums1 + Math.max(nums1[count1 - 1], nums2[count2 - 1]); // Maximum of left side + if ((len1 + len2) % 2 == 1) { + return result; // If odd, return the max of the left side } - // ����ż����������� - int nextValue = (count1 == len1) ? nums2[count2]: - (count2 == len2) ? nums1[count1]: + // If even, find the next smallest element from the right side + int nextValue = (count1 == len1) ? nums2[count2] : + (count2 == len2) ? nums1[count1] : Math.min(nums1[count1], nums2[count2]); - return (result + nextValue) / 2.0; + + return (result + nextValue) / 2.0; // Return the average of left and right sides } } - return Integer.MIN_VALUE; // ���Ե��������� - } - - // ��������true,ż������false - private boolean isOdd(int x) { - return (x & 1) == 1; + return Integer.MIN_VALUE; // This should never happen if input arrays are valid } }