class Solution {
public:
int singleNumber(int A[], int n) {
int sign = 0;
long long ret = 0;
for(int i = 0; i < n ;i++)
{
long long c = A[i];
if(A[i] < 0)
{
sign++;
ret = threeXOR(ret,-c);
}
else
ret = threeXOR(ret,c);
}
sign %= 3;
if(sign != 0)
ret = -ret;
return ret;
}
long long threeXOR(long long a, long long b)
{
long long result = 0;
long long pos = 1;
while(a > 0 || b > 0)
{
int r = 0;
r += (a%3);
r += (b%3);
a/=3;
b/=3;
r %= 3;
result += r*pos;
pos*=3;
}
return result;
}
};
leetcode:Single Number II
最新推荐文章于 2017-08-21 13:07:00 发布