Code run correct in my computer,but have a Runtime Error in leetcode


  • 0
    K
    public class Solution {
    int orderStart=0,orderEnd=0;
    int midnumStart=0,midnumEnd=0;
    float midnum=0;
    int offset=1,index=0;
    public float findMedianSortedArrays(int[] nums1, int[] nums2){
    	if(nums2.length==0){
    		if(nums1.length%2==0)
    	        return (float)(nums1[nums1.length/2]+nums1[nums1.length/2-1])/2;
    	    else return nums1[nums1.length/2];
    	}else if(nums1.length==0){
    		if(nums2.length%2==0)
    	        return (float)(nums2[nums2.length/2]+nums2[nums2.length/2-1])/2;
    	    else
    	        return nums2[nums2.length/2];
    	}else if(nums1.length==1&&nums2.length==1){
    	    return (float)(nums2[0]+nums1[0])/2;
    	}
    	else{
    		if((nums1.length+nums2.length)%2==0){
    			midnumStart=(nums1.length+nums2.length)/2;
    			midnumEnd=midnumStart+1;
    		}else{
    			midnumStart=(nums1.length+nums2.length)/2+1;
    			midnumEnd=midnumStart;
    		}
    		int median=0;
    		int start1=0,end1=nums1.length-1,start2=0,end2=nums2.length-1;
    		while(offset!=0){	
    			median=(start1+end1)/2;
    			binaryChop(nums1,median,nums2,start2,end2);
    			if(offset==0){
    				return midnum;
    			}else if(offset>0){//如果中点值偏大
    				end1=median-1>=start1?median-1:median;//更新end位置
    				if(end1-offset>start1)	start1=end1-offset;//根据误差修正start和end的值
    				//if(orderStart-1<end2)	end2=orderStart-1;
    				//if(end2-offset>start2)	start2=end2-offset;
    			}else{//如果中点值偏小
    				start1=median+1<=end1?median+1:median;
    				if(start1-offset<end1)	end1=start1-offset;//根据误差修正start和end的值
    				//if(orderEnd-1>start2)	start2=orderEnd-1;
    				//if(start2-offset<end2)	end2=start2-offset;
    			}
    			median=(start2+end2)/2;
    			binaryChop(nums2,median,nums1,start1,end1);
    			if(offset==0){
    				return midnum;
    			}else if(offset>0){//如果中点值偏小
    				end2=median-1>=start2?median-1:median;//更新end位置
    				if(end2-offset>start2)	start2=end2-offset;//根据误差修正start和end的值
    				//if(orderStart-1<end1)end1=orderStart-1;
    				//if(end1-offset>start1)	start1=end1-offset;
    			}else{//如果中点值偏小
    				start2=median+1<=end2?median+1:median;
    				if(start2-offset<end2)	end2=start2-offset;//根据误差修正start和end的值
    				//if(orderEnd-1>start1)start1=orderEnd-1;
    				//if(start1-offset<end1)	end1=start1-offset;
    			}				
    		}
    	}
    	return midnum;
    }
    public void binaryChop(int array[],int index,int numArray[],int start,int end){//二分查找当前index值在numArray数组中的位置
    	int i=0,mid=(start+end)/2,num=array[index];
    	while(start<=end){
    		if(numArray[mid]==num){
    			orderStart=mid;orderEnd=mid;
    			//return mid+1;
    			while(orderStart-1>0&&numArray[orderStart-1]==num) orderStart--;
    			while(orderEnd+1<numArray.length&&numArray[orderEnd+1]==num) orderEnd++;
    			//orderStart++;
    			orderEnd++;
    			break;
    		}
    		else if(numArray[mid]<num){
    			start=mid+1;
    			mid=(start+end)/2;
    		}
    		else{
    			end=mid-1;
    			mid=(start+end)/2;
    		}
    	}
    	if(numArray[mid]!=num){
    		if(numArray[mid]>num){
    			orderStart=mid;orderEnd=mid;
    		}
    		else{
    			orderStart=mid+1;orderEnd=mid+1;
    		}
    	}
    	if(orderStart+index+1>midnumEnd){
    		offset=orderStart+index+1-midnumEnd;
    		return ;
    	}else if(orderEnd+index+1<midnumStart){
    		offset=orderEnd+index-midnumStart;
    		return ;
    	}else if(orderEnd+index+1>=midnumEnd&&orderStart+index+1<=midnumStart){
    		offset=0;
    		midnum=array[index];
    		return;
    	}else if(orderStart+index+1==midnumEnd){
    		offset=0;
    		if((array.length+numArray.length)%2==1)midnum=array[index];
    		else{
    			if(index-1<0){
    				midnum=(float)(array[index]+numArray[orderStart-1])/2;
    				return;
    			}
    			System.out.println(index+","+"orderStart");
    			if(array[index-1]<numArray[orderStart-1]){
    				midnum=(float)(array[index]+numArray[orderStart-1])/2;
    				return;
    			}
    			else{
    				midnum=(float)(array[index]+array[index-1])/2;
    				return;
    			}
    		}
    	}else if(orderEnd+index+1==midnumStart){
    		offset=0;
    		if((array.length+numArray.length)%2==1)midnum=array[index];
    		else{
    			if(index+1>=array.length){
    				midnum=(float)(array[index]+numArray[orderEnd])/2;
    				return;
    			}
    			if(array[index+1]>numArray[orderEnd]){
    				midnum=(float)(array[index]+numArray[orderEnd])/2;
    				return;
    			}
    			else{
    				midnum=(float)(array[index]+array[index+1])/2;
    				return;
    			}
    		}
    	}
    }
    

    }
    the test of [6,7], [1,2,3,4,5,8] have a Runtime Error Message: Line 114: java.lang.ArrayIndexOutOfBoundsException: -1,but In my compute ,the answer is right.


  • 0

    As all test cases are run in the same process, please avoid using class variables such as orderStart, orderEnd, midnumStart, ... If you have to use class variables, remember to reset them in the very first line of findMedianSortedArrays function.

    For more details, please read the FAQ.


  • 0
    K

    Oh,I think I understand the error,i will try to reset them,I hope I can get the right answer.thank yor.


Log in to reply
 

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