File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 226
226
279|[ Perfect Squares] ( ./0279-perfect-squares.js ) |Medium|
227
227
282|[ Expression Add Operators] ( ./0282-expression-add-operators.js ) |Hard|
228
228
283|[ Move Zeroes] ( ./0283-move-zeroes.js ) |Easy|
229
+ 287|[ Find the Duplicate Number] ( ./0287-find-the-duplicate-number.js ) |Medium|
229
230
289|[ Game of Life] ( ./0289-game-of-life.js ) |Medium|
230
231
290|[ Word Pattern] ( ./0290-word-pattern.js ) |Easy|
231
232
292|[ Nim Game] ( ./0292-nim-game.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments