LeetCode OJ 581 Shortest Unsorted Continuous Subarray [Easy]

来源:互联网 发布:com口测试软件 编辑:程序博客网 时间:2024/06/06 07:52

题目描述:

Given an integerarray, you need to find one continuous subarray that if you only sort thissubarray in ascending order, then the whole array will be sorted in ascendingorder, too.

You need to find theshortest such subarray and output its length.

Example 1:

Input: [2, 6, 4, 8,10, 9, 15]

Output: 5

Explanation: Youneed to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sortedin ascending order.

Note:

Then length of theinput array is in range [1, 10,000].

The input array maycontain duplicates, so ascending order here means <=.

题目理解:

给定一个整数数字,找出最短的这样一个连续的数字串,使得其按照升序排序后,整个数字串都是升序排列,返回找到的数字串的长度

题目分析:

1.  将这个数字串分成3个部分,A升序序列部分,B乱序序列部分,C升序序列部分

2.  A序列的最后一个数字 <= B序列中的最小值,

B序列中的最大值 <= C序列的第一个数字;

我的解答:

//B序列以start开头,以end结束//start的前一个数(A的最后一个)必须比B序列最小的数小//end的后一个数(C的第一个) 必须比B序列最大的数大static public int findUnsortedSubarray(int[] nums) {    int start = -1;    int end = -1;    int max = Integer.MIN_VALUE;    int min = Integer.MAX_VALUE;    for (int i = 0; i < nums.length - 1; i++) {        if (nums[i] > nums[i + 1]) {            end = i + 1;            if (start == -1){                start = i;            }            //找到B序列中最大与最小值            for(int j = start; j <= end; j++){                if(nums[j] < min){                    min = nums[j];                }                if(nums[j] > max){                    max = nums[j];                }            }            //start向前找,确定start的位置            for(int j = start; j >= 0 && nums[j] > min; j--){                start = j;            }            //end向后找,确定end的位置,有可能end更小,所以再一次确定start            for(int j = end; j < nums.length && nums[j] < max; j++){                end = j;                i = j;                if (nums[j] < min){                    min = nums[j];                }            }            //start向前找,再次确定start的位置            for(int j = start; j >= 0 && nums[j] > min; j--){                start = j;            }        }    }    if(start != end)        return end - start + 1;    else        return 0;}

更好的解答:

I use the variables beg and end to keep track of minimum subarray A[beg...end] whichmust be sorted for the entire array A to be sorted. If end< beg < 0 at the end ofthe forloop, then the array is already fully sorted.

public int findUnsortedSubarray(int[] A) {        int n = A.length, beg = -1, end = -2, min = A[n-1], max = A[0];        for (int i=1;i<n;i++) {        max = Math.max(max, A[i]);        min = Math.min(min, A[n-1-i]);        if (A[i] < max) end = i;        if (A[n-1-i] > min) beg = n-1-i;        }        return end - beg + 1;}

其中end = -2是特别聪明的做法,防止了beg = -1的情况下返回-1,如下是end= -1时最后判断了一下beg是否等于-1:

public class Solution {    public int findUnsortedSubarray(int[] nums) {        int len=nums.length;        int max=Integer.MIN_VALUE, min=Integer.MAX_VALUE;        int start=-1, end=-1;        for(int i=0; i<len; i++){            max = Math.max(max, nums[i]); //from left to right, search the current max            min = Math.min(min, nums[len-i-1]);  //from right to left, search the current min             if(nums[i] < max)                end = i;            if(nums[len-i-1] > min)                start = len-i-1;        }        if(start==-1) //the entire array is already sorted            return 0;        return end-start+1;    }}





阅读全文
0 0
原创粉丝点击