原地哈希,利用下标值作为哈希的一部分
class Solution:
def minNumberDisappeared(self , nums: List[int]) -> int:
# write code here
# 原地哈希
res = -1
if not nums:
return 1
len_nums = len(nums)
# 先遍历数组将所有小于0的元素都改成n + 1
for i in range(len_nums):
if nums[i] < 0:
nums[i] = len_nums + 1
for i in range(len_nums):
temp = abs(nums[i])
if temp > 0 and temp <= len_nums:
nums[temp - 1] = - abs(nums[temp - 1])
for i in range(len_nums):
if nums[i] > 0:
res = i + 1
break
if i == len_nums - 1 and res == -1:
res = len_nums + 1
return res