leetcode Find the Duplicate Number

题目连接

https://leetcode.com/problems/find-the-duplicate-number/

Find the Duplicate Number

Description

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.

Note: 
You must not modify the array (assume the array is read only). 
You must use only constant, O(1) extra space. 
Your runtime complexity should be less than O(n2). 
There is only one duplicate number in the array, but it could be repeated more than once.

set水过。。

class Solution {
public:
	int findDuplicate(vector<int>& nums) {
		size_t i, n = nums.size();
		unordered_set<int> A;
		for (i = 0; i < n; i++) {
			if (A.find(nums[i]) == A.end()) A.insert(nums[i]);
			else break;
		}
		return nums[i];
	}
};
时间: 2024-08-09 10:43:53

leetcode Find the Duplicate Number的相关文章

[LeetCode] Find the Duplicate Number 寻找重复数

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate element must exist. Assume that there is only one duplicate number, find the duplicate one. Note: You must not modify t

LeetCode 287. Find the Duplicate Number

Find the Duplicate Number | LeetCode OJhttps://leetcode.com/problems/find-the-duplicate-number/ 这个题目属于编码比较简单但解法分析过程比较复杂. 首先,把1~n放入0~n个元素,必定有两个或以上元素重复.数字里没有0,所以从下标0出发不会再回到最初的元素0. 假设当前的下标为x,下一步的下标为f(x),若f(x) = A[x], 也即每次跳到一个元素,则下一步移动到当前元素值对应的元素下标.我们来证明

LeetCode 287. Find the Duplicate Number (找到重复的数字)

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Note: You must not modify th

[LeetCode] 287. Find the Duplicate Number 寻找重复数

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Example 1: Input: [1,3,4,2,2

LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))

LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过不了的,但是可能判题并没有卡空间复杂度,所以也能AC. class Solution: # 基本思路为,将第一次出现的数字 def findDuplicate(self, nums: List[int]) -> int: s = set() for i in nums: a = i in s if a

[LeetCode] 287. Find the Duplicate Number(Floyd判圈算法)

传送门 Description Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Note: You mu

&lt;LeetCode OJ&gt; 287. Find the Duplicate Number

287. Find the Duplicate Number My Submissions Question Total Accepted: 18097 Total Submissions: 48596 Difficulty: Hard Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate

【LeetCode从零单刷】Find the Duplicate Number

题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Note: You must not modif

leetcode 217. Contains Duplicate 287. Find the Duplicate Number

217. Contains Duplicate 后面3个题都是限制在1-n的 class Solution { public: bool containsDuplicate(vector<int>& nums) { int length = nums.size(); if(length <= 0) return false; sort(nums.begin(),nums.end()); for(int i = 1;i < length;i++){ if(nums[i] ==