-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
31 lines (29 loc) · 875 Bytes
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.company;
public class Main {
public static void main(String[] args) {
// write your code here
}
public int findDuplicate_FloydTortoiseAndHase(int[] nums) {
int tortoise = 0, hase = 0;
boolean firstMeet = true;
while (tortoise != hase || firstMeet){
tortoise = nums[tortoise];
hase = nums[hase];
hase = nums[hase];
if(tortoise == hase) firstMeet = false;
}
hase = 0;
while (tortoise != hase){
tortoise = nums[tortoise];
hase = nums[hase];
}
return hase;
}
public int findDuplicate_ModifyArray(int[] nums) {
for (int i = 0; i < nums.length; i++) {
if(nums[Math.abs(nums[i])] < 0) return Math.abs(nums[i]);
nums[Math.abs(nums[i])] *= -1;
}
return -1;
}
}