其他分享
首页 > 其他分享> > 布尔代数的运算

布尔代数的运算

作者:互联网

布尔运算

~ 对应于逻辑运算NOT
& 对应于逻辑运算AND
| 对应于逻辑运算OR
^ 对应于逻辑运算异或

布尔运算|对&的分配律:a | (b & c)=(a | b) & (a | c)
布尔运算&对|的分配律:a & (b | c)=(a & b) | (a & c)
对于任何值a,a ^ a=0(用0来表示全0的位向量)(a ^ b) ^ a=b

136. Single NumberEasy

Given a non-empty array of integers, every element appears twice except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Example 1:
Input: [2,2,1]
Output: 1

Example 2:
Input: [4,1,2,1,2]
Output: 4

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int num=0;
        for(int i=0;i<nums.size();++i)
        {
            num = num ^ nums[i];  //进行一系列布尔运算 ^ ,最终只剩下出现一次的数 
        }
        return num;
    }
};

标签:逻辑运算,运算,布尔运算,布尔代数,int,分配律,Output,对应
来源: https://blog.csdn.net/meng199703/article/details/98490430