#用DP写的 #dp[i]=dp[j]+1, 其中j是符合j<i且arr[j]=arr[i]-1条件的j的最大值 class Solution(): def LongestContinuousSubsequence(self,arr): dp=[0]*len(arr) for i in range(len(arr)): index=-1 for j in range(i,-1,-1): if arr[j]==arr[i]-1: index=j break if index>0: dp[i]=dp[index]+1 else: dp[i]=1 print dp max_index=dp.index(max(dp)) pre_maxvalue=max(dp) res=[max_index] for i in range(max_index-1,-1,-1): if dp[i]==pre_maxvalue-1: res=[i]+res pre_maxvalue=dp[i] return res