例如:
[1,4,5,3,2,8]
返回4,解釋:需要排序的是[4,5,3,2]
思路:
雙指針操作
從左邊遍歷尚辑,找出不合理的位置北滥,此位置為right_index
從右邊遍歷,找出不合理的位置蜀细,此位置為left_index
right_index - left_index + 1
class Solution(object):
def run(self, lst):
n = len(lst)
_max = lst[0]
for i in range(0, n):
if lst[i] > _max:
_max = lst[i]
else:
k = i # k是需要排序的最右邊的index
print ("k=", k)
_min = lst[-1]
for i in range(n-1, 0, -1):
if lst[i] < _min:
_min = lst[i]
else:
j = i # j是需要排序的最左邊的index
print ("j=", j)
return k-j+1
lst = [1,4,5,3,2,8]
s =Solution()
print (s.run(lst))