-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingle Number.cpp
47 lines (31 loc) · 953 Bytes
/
Single Number.cpp
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
39
40
41
42
43
44
45
46
47
/*
problem link : https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/528/week-1/3283/
problem name:Single Number
status: accepted
author : moahnd sakr
*/
class Solution {
public:
int singleNumber(vector<int>& nums) {
int result=0;
int len=nums.size();
sort(nums.begin(),nums.end());
if(len==1) return nums[0];
for(int i=0;i<len;i++){
if(!i)
{
if(nums[i]!=nums[i+1])
return nums[i];
}
else if(i==len-1){
if(nums[i]!=nums[i-1])
return nums[i];
}
else {
if(nums[i-1]!=nums[i]&&nums[i]!=nums[i+1])
return nums[i];
}
}
return result;
}
};