class Solution: def lenLongestFibSubseq(self, A: List[int]) -> int: s = set(A) n = len(A) result = 0 for i in range(n-1): for j in range(i+1, n): a, b = A[i], A[j] count = 2 while a+b in s: a, b = b, a+b count += 1 # A= [1,2,3,4,5,6,7,8] # [1,2,3,5,8] # [1,3,4,7] # [1,4,5] # ... # 取其中最大的序列长度 result = max(result, count) return result if result > 2 else 0