single-number-ii

来源:互联网 发布:图片毛玻璃效果 软件 编辑:程序博客网 时间:2024/05/22 17:12

题目:

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?

程序:

class Solution {public:    int singleNumber(int A[], int n) {       int one = 0, two = 0, three = 0;        for(int i = 0; i < n; i++)            {            two |= one & A[i];            one ^= A[i];            three = ~(one & two);            one &= three;            two &= three;        }        return one;    }};
原创粉丝点击