Skip to content

Commit 039c2ad

Browse files
committed
Add solution #287
1 parent 81af3aa commit 039c2ad

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@
226226
279|[Perfect Squares](./0279-perfect-squares.js)|Medium|
227227
282|[Expression Add Operators](./0282-expression-add-operators.js)|Hard|
228228
283|[Move Zeroes](./0283-move-zeroes.js)|Easy|
229+
287|[Find the Duplicate Number](./0287-find-the-duplicate-number.js)|Medium|
229230
289|[Game of Life](./0289-game-of-life.js)|Medium|
230231
290|[Word Pattern](./0290-word-pattern.js)|Easy|
231232
292|[Nim Game](./0292-nim-game.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 287. Find the Duplicate Number
3+
* https://leetcode.com/problems/find-the-duplicate-number/
4+
* Difficulty: Medium
5+
*
6+
* Given an array of integers nums containing n + 1 integers where each integer is in the
7+
* range [1, n] inclusive.
8+
*
9+
* There is only one repeated number in nums, return this repeated number.
10+
*
11+
* You must solve the problem without modifying the array nums and using only constant extra space.
12+
*/
13+
14+
/**
15+
* @param {number[]} nums
16+
* @return {number}
17+
*/
18+
var findDuplicate = function(nums) {
19+
let slow = nums[0];
20+
let fast = nums[nums[0]];
21+
22+
while (slow !== fast) {
23+
slow = nums[slow];
24+
fast = nums[nums[fast]];
25+
}
26+
slow = 0;
27+
while (slow !== fast) {
28+
slow = nums[slow];
29+
fast = nums[fast];
30+
}
31+
32+
return slow;
33+
};

0 commit comments

Comments
 (0)