We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Difficulty: 中等
Related Topics: 位运算, 数组, 双指针, 二分查找
给定一个包含 n + 1 个整数的数组 nums ,其数字都在 [1, n] 范围内(包括 1 和 n),可知至少存在一个重复的整数。
n + 1
nums
[1, n]
1
n
假设 nums 只有 一个重复的整数 ,返回 这个重复的数 。
你设计的解决方案必须 不修改 数组 nums 且只用常量级 O(1) 的额外空间。
O(1)
示例 1:
输入:nums = [1,3,4,2,2] 输出:2
示例 2:
输入:nums = [3,1,3,4,2] 输出:3
提示:
nums.length == n + 1
1 <= nums[i] <= n
进阶:
O(n)
Language: JavaScript
/** * @param {number[]} nums * @return {number} */ var findDuplicate = function(nums) { let set = new Set() for (let item of nums) { if (set.has(item)) return item set.add(item) } return -1 };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
287. 寻找重复数
Description
Difficulty: 中等
Related Topics: 位运算, 数组, 双指针, 二分查找
给定一个包含
n + 1
个整数的数组nums
,其数字都在[1, n]
范围内(包括1
和n
),可知至少存在一个重复的整数。假设
nums
只有 一个重复的整数 ,返回 这个重复的数 。你设计的解决方案必须 不修改 数组
nums
且只用常量级O(1)
的额外空间。示例 1:
示例 2:
提示:
nums.length == n + 1
1 <= nums[i] <= n
nums
中 只有一个整数 出现 两次或多次 ,其余整数均只出现 一次进阶:
nums
中至少存在一个重复的数字?O(n)
的解决方案吗?Solution
Language: JavaScript
The text was updated successfully, but these errors were encountered: