4ms Solution with JAVA


  • -4
    E
    public class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        double now=0;
        double pre=0;
        int n = (nums1.length+nums2.length)/2+1;
        int i=0;
        int j=0;
        int k=0;
        while(k<n){
        	pre = now;
    		if(i==nums1.length){
    			now = nums2[j++];
    		}
    		else if(j==nums2.length){
    			now = nums1[i++];
    		}
    		else now = nums1[i]<nums2[j]?nums1[i++]:nums2[j++];
    		k++;
        }
        return (nums1.length+nums2.length)%2==1?now:(pre+now)/2;
    }
    

    }


  • 7
    G

    Does this pass the time limit when running test? This is O(n) solution (O((m+n)/2+1) if you want exact number). The question is asking for O(log(m+n)) solution.


  • 0
    E

    You are right, I missed the requirement of time complexity. And this solution is accepted by online judgement.


  • 0
    J

    how it could be accepted...time limit is the key.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.