leetcode287 Find the Duplicate Number

 1 """
 2 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.
 3 Example 1:
 4 Input: [1,3,4,2,2]
 5 Output: 2
 6 Example 2:
 7 Input: [3,1,3,4,2]
 8 Output: 3
 9 """
10 """
11 解法一:
12 这道题竟然可以用快慢指针法!!!
13 与leetcode142类似。https://www.cnblogs.com/yawenw/p/12324170.html
14 因此找环 找重复值 都可以考虑用快慢指针法
15 """
16 class Solution1:
17     def findDuplicate(self, nums):
18         if not nums:
19             return -1
20         slow = nums[0]
21         fast = nums[nums[0]]
22         while slow != fast:
23             slow = nums[slow]
24             fast = nums[nums[fast]]
25         fast = 0
26         while slow != fast:
27             fast = nums[fast]
28             slow = nums[slow]
29         return slow
30
31 """
32 解法二:时间复杂度高的解法
33 """
34 class Solution2:
35     def findDuplicate(self, nums):
36         for i in range(len(nums)):
37             if nums[i] in nums[i+1:]:
38                 return nums[i]

原文地址:https://www.cnblogs.com/yawenw/p/12432029.html

时间: 2024-08-15 01:37:37

leetcode287 Find the Duplicate Number的相关文章

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

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.

[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

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], 也即每次跳到一个元素,则下一步移动到当前元素值对应的元素下标.我们来证明

Find the Duplicate Number 解答

Question 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 

<LeetCode OJ> 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

287. Find the Duplicate Number *HARD*

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-Cycle Detection,Find the Duplicate Number

      本来以为这题很简单,但是看要求,第一不能改变这个数组,第二只能使用O(1)的空间,第三时间复杂度小于O(n^2),就不能使用遍历数组的方式来解决了. 有两种方法,一种是利用Binary Search,一种是利用Floyd的cycle detection算法. Binary Search Method This method is based on a theroy called Pigeonhole princinpal. In mathematics,Pigeonhole prin