- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle_num.java
More file actions
Latest commit
30 lines (27 loc) · 708 Bytes
/
Copy pathsingle_num.java
File metadata and controls
30 lines (27 loc) · 708 Bytes
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
//Link: https://leetcode.com/problems/single-number/
classSolution {
publicintsingleNumber(int[] nums) {
Arrays.sort(nums);
if(nums.length==1){
returnnums[0];
}
else{
for(inti=0;i<nums.length;i++){
if(i==0){
if(nums[i]!=nums[i+1]){
returnnums[i];
}
}
elseif(i==nums.length-1){
returnnums[i];
}
else{
if((nums[i-1]!=nums[i])&&(nums[i+1]!=nums[i])){
returnnums[i];
}
}
}
}
return0;
}
}