思路1:使用顺序查找
1 class Solution: 2 def fixedPoint(self, A: 'List[int]') -> int: 3 for i in range(len(A)): 4 if i == A[i]: 5 return i 6 return -1
思路2:使用二分查找
1 class Solution: 2 def fixedPoint(self, A: 'List[int]') -> int: 3 n = len(A) 4 i = 0 5 j = n - 1 6 while i <=j: 7 mid = i + (j - i) // 2 8 if A[mid] == mid: 9 return mid 10 elif A[mid] < mid: 11 i = mid + 1 12 else: 13 j = mid - 1 14 return -1