-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingle Number solution2.cpp
More file actions
42 lines (36 loc) · 928 Bytes
/
Copy pathSingle Number solution2.cpp
File metadata and controls
42 lines (36 loc) · 928 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
31
32
33
34
35
36
37
38
39
40
41
42
/*
problem link :https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/549/
problem name: Single Number
status: accepted
author : moahnd sakr
*/
class Solution {
public:
int singleNumber(vector<int>& nums) {
sort(nums.begin(),nums.end());
int len=nums.size();
int result=0;
if(len>1){
if(nums[0]!=nums[1]) result=nums[0];
if(nums[len-2]!=nums[len-1]) result=nums[len-1];
else {
for(int i=1;i<len-2;i++){
int val=0;
if(nums[i-1]==nums[i]){
++val;
}
if(nums[i]==nums[i+1]){
++val;
}
if(!val){
result=nums[i];
}
}
}
}
else {
return nums[0];
}
return result;
}
};