leetcode 581[easy]---- Shortest Unsorted Continuous Subarray

难度:easy

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

You need to find the shortest such subarray and output its length.

leetcode 581[easy]---- Shortest Unsorted Continuous Subarray

思路:找出需要重新进行排列的最小数列数,使array变成有序array。

           唯有最小的数已经在array最左边,最大的数已经在array最右边,这样的element才不需要变动,只需要变动中间的即可。用sorted()函数,将排序好的array赋予一个新的array,注意此处用sorted()函数,而不是 array.sort(),后者只在原函数内部修改函数,但不能赋值给新的array。

          将原array与排序号的array的最左和最右分别进行比较,最后减掉首尾两个array相同位置相同的数,中间即为需要重新排序的序列

          第一个code是自己写的,看着自己都嫌弃,太繁琐了。第二个code是借鉴别人的,思路是一样的,但是简单流畅太多了。


leetcode 581[easy]---- Shortest Unsorted Continuous Subarray

    leetcode 581[easy]---- Shortest Unsorted Continuous Subarray

 方法2: