150强访谈简单

找到重复的数字

“查找重复数字”问题的详细指南和 Python 实现。

问题陈述

简单

给定一个包含 n + 1 个整数的整数数组 nums,其中每个整数都在 [1, n] 范围内(含)。

nums 中只有一个重复的数字,返回这个重复的数字。

您必须在不修改数组 nums 的情况下仅使用恒定的额外空间来解决该问题。

实现一个返回重复数字的函数 findDuplicate(nums: list) -> int

约束条件
  • 1 <= n <= 100000
  • nums.length == n + 1
  • 1 <= nums[i] <= n
  • There is only one repeated number in nums, but it could be repeated more than once

示例

Example 1
Input
[1,3,4,2,2]
Output
2
Explanation

The duplicate number is 2. It appears twice in the array.

Example 2
Input
[3,1,3,4,2]
Output
3
Explanation

The duplicate number is 3. It appears twice in the array.

Example 3
Input
[3,3,3,3,3]
Output
3
Explanation

The duplicate number is 3. It appears five times.

Need a Hint?
考虑使用特定于链接列表的数据结构,例如集合或堆。
Edge Cases to Watch
  • 空输入结构
  • 单元素输入
  • 大数值范围

准备好解决了吗?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在编辑器中打开
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推荐的 Python 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。