leetcode 581. 最短无序连续子数组

class Solution {
    public int findUnsortedSubarray(int[] arr) {
        if(arr == null || arr.length < 2){
			return 0;
		}
		int max = Integer.MIN_VALUE;
		int min = Integer.MAX_VALUE;
		int R = 0;
		int L = 0;
		for (int i = 0; i < arr.length; i++) {
			if(max > arr[i]) {
				R = i;
			}
			max = Math.max(max, arr[i]);
		}
		for (int i = arr.length - 1; i >= 0; i--) {
			if(min < arr[i]) {
				L = i;
			}
			min = Math.min(min, arr[i]);
		}
		return R == L ? 0 : R - L + 1;	
    }
}

你可能感兴趣的:(leetcode,java)