Java Solution,List and Sort


  • 0
    public class Solution {
        public int findUnsortedSubarray(int[] nums) {
                    List<Integer> list = new ArrayList<Integer>(); // can use Arrays.copyof().
    		List<Integer> res = new ArrayList<Integer>();
    		for(int i = 0; i < nums.length; i++)
    			list.add(i, nums[i]);
    		
    		Arrays.sort(nums);
    		
    		for(int i = 0; i < nums.length; i++){
    			
    			int t = list.get(i);
    			if(nums[i] != t)
    				res.add(i);
    		}
    		if(res.size() == 0) return 0;
    		return (res.get(res.size() - 1) - res.get(0)) + 1;
    		
        }
    }
    

Log in to reply
 

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