Single Number II, singlenumberii
Given an array of integers, every element appearsThreeTimes has t for one. Find that single one.
Note:
Your algorithm shocould have a linear runtime complexity. cocould you implement it without using extra memory?
Solution:
1. Using bitwise operations in binary, if n * 3 identical numbers are added, each bit of binary must be a multiple of 3.
2. The remaining separate number is the remainder of each digit divided by 3. Then, each digit is converted to a decimal number, and the sum is the separate number.
1 int singleNumber (int * nums, int numsSize) {2 int ary [32] = {0}; 3 int res = 0; 4 int I; 5 int j; 6 for (I = 0; I <32; I ++) 7 {8 for (j = 0; j <numsSize; j ++) 9 {10 ary [I] + = (nums [j]> I) & 1); 11 ary [I] = ary [I] % 3; 12} 13 res | = (ary [I] <I); 14} 15 return res; 16}
Turn: http://blog.csdn.net/feliciafay/article/details/19004479