[This article is from the Sky Cloud-owned blog Park]
Title: There is an array that asks him for the maximum (longest) continuous interval (the number is a continuous interval).
My solution is as follows:
classFinder (object):" "determine whether two contiguous digits are contiguous, if continuous: 1. Continue to Judge 2. Record continuous length finally return maximum continuous length" " defFind_continuity (self,index,array,length=1): ifIndex+1 <Len (array): _curr=Array[index] _next= Array[index+1] ifABS (_NEXT-_CURR) = = 1: Length+ = 1length= Self.find_continuity (index+1, Array,length)returnlength" "The index value at the beginning of each number and the maximum continuous interval length are stored in the dictionary to find the largest key value in the dictionary, that is, the maximum continuous interval returns the corresponding array slice based on the index and length of the maximum contiguous interval" " deffind_longest (Self,array): Continuity=dict () forIinchRange (len (array)): Length=self.find_continuity (I,array) continuity[i]=length Longest= Max (Continuity.items (), key=LambdaX:x[1]) Index,length= Longest[0],longest[1] returnarray[index:index+Length]if __name__=='__main__': Array= [1,2,3,4,5,4,2,5,3,4,7,1,5,9,10,9,8,7,6,5,4,3,4,5,6,8] Finder=Finder () Longest_array=finder.find_longest (Array)Print(Longest_array)
Python version to find the maximum contiguous range of arrays