-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingle_Number_II.cpp
More file actions
32 lines (28 loc) · 888 Bytes
/
Single_Number_II.cpp
File metadata and controls
32 lines (28 loc) · 888 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
// Source : https://oj.leetcode.com/problems/word-break-ii/
// Author : zheng yi xiong
// Date : 2014-21-02
/**********************************************************************************
*
* Given an array of integers, every element appears three times except for one. Find that single one.
* Note:
* Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
*
**********************************************************************************/
#include "stdafx.h"
class CSingle_Number_II {
public:
int singleNumber(int A[], int n) {
int odd = 0; //三次中出现奇数次的数位
int even = 0; //三次中出现偶数次的数位
int temp = 0; //消除出现三次的数位
for (int i = 0; i < n; ++i)
{
even ^= odd & A[i];
odd ^= A[i];
temp = ~(odd & even);
even &= temp;
odd &= temp;
}
return odd;
}
};