-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
38 lines (37 loc) · 1 KB
/
Copy pathsolution.java
File metadata and controls
38 lines (37 loc) · 1 KB
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
32
33
34
35
36
37
38
// 287. Find the Duplicate Number
// https://leetcode.com/problems/find-the-duplicate-number/
// Medium | Java | Accepted 2025-11-12
// Runtime 55 ms | Memory 79.1 MB
class Solution {
public int findDuplicate(int[] nums) {
Arrays.sort(nums);
for(int i = 0; i<nums.length; i++)
{
int find = nums[i];
int start = 0;
int end = nums.length-1;
int mid = (start+end)/2;
while(start<=end)
{
mid = (start+end)/2;
if(nums[mid]==find)
{
break;
}
else if(nums[mid]<find)
{
start = mid+1;
}
else
{
end = mid-1;
}
}
if((mid-1>=0 && nums[mid-1]==find) || (mid+1 <nums.length && nums[mid+1]==find))
{
return find;
}
}
return -1;
}
}