AC Java Solution


  • 0
    G
    public static int findUnsortedSubarray(int[] nums) {
        int pre[]=new int[nums.length];
    	for(int i=0;i<nums.length;i++){
    		pre[i]=nums[i];
    	}
    	Arrays.sort(pre);//nums is unsorted, and pre is sorted
    	int left=0;
    	int right=nums.length-1;
    	while(left<right){// comparing the two arrays
    	    if(pre[left]==nums[left] && pre[right]==nums[right]){
    			left++;
    			right--;
    		}else if(pre[left]==nums[left]){
    			left++;
    		}else if(pre[right]==nums[right]){
    			right--;
    		}else{
    			break;
    		}
    	}
    	return (right-left==0) ? 0 : right-left+1;
    }

Log in to reply
 

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