File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -23,3 +23,27 @@ pub fn find_duplicate(nums: Vec<i32>) -> i32 {
23
23
24
24
ptr1
25
25
}
26
+
27
+ /*
28
+ Algorithm - Floyd's Tortoise and Hare (Cycle Detection)
29
+ ------------------------------------------------------
30
+ 1. Initialize tortoise and hare to the first element of the array.
31
+ 2. Move tortoise to nums[tortoise].
32
+ 3. Move hare to nums[nums[hare]].
33
+ 4. Repeat steps 2 and 3 until tortoise == hare.
34
+ 5. Initialize ptr1 to the first element of the array.
35
+ 6. Initialize ptr2 to tortoise.
36
+ 7. Move ptr1 to nums[ptr1].
37
+ 8. Move ptr2 to nums[ptr2].
38
+ 9. Repeat steps 7 and 8 until ptr1 == ptr2.
39
+ 10. Return ptr1.
40
+
41
+ Time Complexity: O(n)
42
+ Space Complexity: O(1)
43
+
44
+ How works Floyd's Tortoise and Hare?
45
+ ------------------------------------
46
+ https://youtu.be/wjYnzkAhcNk?t=145
47
+
48
+
49
+ */
You can’t perform that action at this time.
0 commit comments