JS solution 212ms


  • 0
    C
    /**
     * @param {number[]} nums
     * @return {number}
     */
    var findUnsortedSubarray = function(nums) {
    	if(nums.length === 0 || nums.length === 1 ) return 0;
    	var sorted = [].sort.call(Array.from(nums),function(a,b){return a-b;});
    	var front = 0,
    	back = nums.length-1;
    
    	while(front <= back){
    		if(nums[front] === sorted[front]) {
    			front +=1 ;
    		} else {
    			break;
    		}
    	}
    	while(back >= front){
    		if(nums[back] === sorted[back]) {
    			back-=1;
    		} else {
    			break;
    		}
    	}
    
    	return back-front + 1;
    };
    

Log in to reply
 

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