当前位置:网站首页>【LeetCode】287. Find duplicates

【LeetCode】287. Find duplicates

2022-06-10 01:32:00 LawsonAbs

1. subject

2. thought

Because the title says : == Only A repeated integer ==, This means that , When we keep exchanging , As long as you encounter the same number, you can return .

3. Code

class Solution:
    def findDuplicate(self, nums: List[int]) -> int:        
        for i in range(len(nums)):
            while(i != nums[i]-1):
                idx = nums[i] - 1  # nums[i]  This number should be placed in the subscript position of 
                if nums[i] == nums[idx]: #  Find the result 
                    return nums[i]
                else:
                    nums[i],nums[idx] = nums[idx],nums[i] # swap
        print(nums)
原网站

版权声明
本文为[LawsonAbs]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206100107228112.html