File tree Expand file tree Collapse file tree 1 file changed +6
-15
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +6
-15
lines changed Original file line number Diff line number Diff line change 3
3
import java .util .HashSet ;
4
4
import java .util .Set ;
5
5
6
- /**
7
- * 287. Find the Duplicate Number
8
- *
9
- * Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive),
10
- * prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
11
- *
12
- Note:
13
- You must not modify the array (assume the array is read only).
14
- You must use only constant, O(1) extra space.
15
- Your runtime complexity should be less than O(n2).
16
- There is only one duplicate number in the array, but it could be repeated more than once.
17
-
18
- */
19
6
public class _287 {
20
7
21
8
public static class Solution1 {
22
- /**no-brainer, used O(n) space*/
9
+ /**
10
+ * no-brainer, used O(n) space
11
+ */
23
12
public int findDuplicate (int [] nums ) {
24
13
Set <Integer > set = new HashSet <>();
25
14
int dup = 0 ;
@@ -34,7 +23,9 @@ public int findDuplicate(int[] nums) {
34
23
}
35
24
36
25
public static class Solution2 {
37
- /** O(1) space */
26
+ /**
27
+ * O(1) space
28
+ */
38
29
public int findDuplicate (int [] nums ) {
39
30
int slow = 0 ;
40
31
int fast = 0 ;
You can’t perform that action at this time.
0 commit comments