异或应用


1.  给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符 (异或又名: 不进位加法

a + b = (a ^ b) + (a & b << 1)

2. 数组中,只有一个数出现一次,剩下都出现两次,找出出现一次的数

public class Solution {
    public int singleNumber(int[] nums) {
        int result = 0, n = nums.length;
        for (int i = 0; i < n; i++)
        {
            result ^= nums[i];
        }
        return result;
    }
}

3. 数组中,只有一个数出现一次,剩下都出现三次,找出出现一次的数

Ones,Twos俩个数表示当前位的状态

如果Twos里面ith是1,则ith当前为止出现1的次数除以3的状态是2
如果Ones里面ith是1,则ith目前为止出现1的次数除以3的状态是1
outputinputcomments
onesones ^ nums[i]~twos ones =(ones ^ nums[i]) & ~twostwos
 0-0-不一样处候选
 1110保留
 10012+1 = 3 消除
twostwos ^ nums[i])~onestwos = (twos ^ nums[i]) & ~onesonescomments
 0-0-不一样处候选
 11101个数非奇数,是偶数, keep
 10011个数奇数,非偶数, 消除

public class Solution {
    public int singleNumber(int[] nums) {
        int ones = 0, twos = 0;
        for(int i = 0; i < nums.length; i++){
            ones = (ones ^ nums[i]) & ~twos;
            twos = (twos ^ nums[i]) & ~ones;
        }
        return ones;
    }
}


4.  数组中,只有两个数出现一次,剩下都出现两次,找出出现一次的这两个数
设此两个数为x和y, 对于x和y,一定是其中一个这一位是1,另一个这一位不是1。 对于其他成对出现的元素,要么都在x所在的那个集合,要么在y所在的那个集合。

public class Solution {
    public int[] singleNumber(int[] nums) {
        //用于记录,区分“两个”数组
        int diff = 0;
        for(int i = 0; i < nums.length; i ++) {
            diff ^= nums[i];
        }
       
        //diff & (-diff)就是取diff的最后一位1的位置
        diff &= -diff;
        
        int[] rets = {0, 0}; 
        for(int i = 0; i < nums.length; i ++) {
            //分属两个“不同”的数组
            if ((nums[i] & diff) == 0) {
                rets[0] ^= nums[i];
            }
            else {
                rets[1] ^= nums[i];
            }
        }
        return rets;
    }
}
5. Maximum XOR of Two Numbers in an Array
int findMaximumXOR(vector<int>& nums) {
        int max = 0, mask = 0;
        unordered_set<int> t;
        // search from left to right, find out for each bit is there 
        // two numbers that has different value
        for (int i = 31; i >= 0; i--){
            // mask contains the bits considered so far
            mask |= (1 << i);
            t.clear();
            // store prefix of all number with right i bits discarded
            for (int n: nums){
                t.insert(mask & n);
            }
            
            // now find out if there are two prefix with different i-th bit
            // if there is, the new max should be current max with one 1 bit at i-th position, which is candidate
            // and the two prefix, say A and B, satisfies:
            // A ^ B = candidate
            // so we also have A ^ candidate = B or B ^ candidate = A
            // thus we can use this method to find out if such A and B exists in the set 
            int candidate = max | (1<<i);
            for (int prefix : t){
                if (t.find(prefix ^ candidate) != t.end()){
                    max = candidate;
                    break;
                }
                
            }
        }
        return max;
    }
Sample:
input1010111101100011
output1100
init0[] 0
imasksetcandidate max
310001000
'0000
10001000
211001000
1100
0100
0011
11001100
111101010
1110
0110
0010
11101100
011111010
1111
0110
0011
11011100


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值