My java sorting solution


  • 0
    C
    public class Solution {
    public int findUnsortedSubarray(int[] nums) {
        
        if(nums == null || nums.length == 1)
        {
            return 0;
        }
        
        if(nums.length == 2 && nums[0]>nums[1])
        {
            return 2;
        }
        
        int [] tmp = new int [nums.length];
        
        int start = 0;
        int end = 0;
        
        
        for(int i=0;i<nums.length;i++)
        {
            tmp[i] = nums[i];
        }
        
        Arrays.sort(nums);
        
        for(int j=0;j<nums.length;j++)
        {
            if((tmp[j] - nums[j]) != 0)
            {
                start = j;
                break;
            }
            
        }
        
        for(int j=nums.length-1;j>-1;j--)
        {
            if((tmp[j] - nums[j]) != 0)
            {
                end = j;
                break;
            }
            
        }
        
        if(start == end)
        {
            return 0;
        }
        
        else
        {
            return end-start+1;   
        }
    }
    

    }


Log in to reply
 

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