-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingle_Number.cc
More file actions
40 lines (37 loc) · 847 Bytes
/
Copy pathSingle_Number.cc
File metadata and controls
40 lines (37 loc) · 847 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
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int singleNumber(int A[], int n) {
int m = 0, i = 0, j = n-1, t;
if (n == 1) return A[0];
while (i != j) {
if (j != m) {
if (A[j] <= A[m]) {
t = A[j];
A[j] = A[m];
A[m] = t;
m = j;
} else --j;
} else {
if (A[i] > A[m]) {
t = A[i];
A[i] = A[m];
A[m] = t;
m = i;
} else ++i;
}
}
if (i % 2 == 0)
return singleNumber(A, i+1);
else
return singleNumber(A+i+1, n-i-1);
}
};
int main(int argc, char **argv) {
Solution s;
int A[] = {2, 3, 4, 2, 1, 3, 5, 5, 4, 1, 9 ,11, 9};
s.singleNumber(A, 13);
cout << s.singleNumber(A, 13) << endl;
}