Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用位运算符仅查找一次数组中存在的数字_C++_Arrays_Bitwise Operators - Fatal编程技术网

C++ 使用位运算符仅查找一次数组中存在的数字

C++ 使用位运算符仅查找一次数组中存在的数字,c++,arrays,bitwise-operators,C++,Arrays,Bitwise Operators,如果我们有一个数组,其中包含两次出现的数字,以及一个只出现一次的数字,我们可以使用XOR运算符根据通信定律查找它。e、 g 1 2 3 2 3 1 ^ 2 ^ 3 ^ 2 ^ 3 = ( 2 ^ 2 ) ^ ( 3 ^ 3 ) ^ 1 = 1 但是我们可以使用位技巧来查找数组中出现一次的数字,而其他数字可以出现n次,n>1吗?使用位运算符,不可以,除非n总是偶数 但是还有很多其他方法可以使用,比如对一次出现的项目进行排序和扫描,或者在输入域受到某种限制的情况下将每个项目分配给bucket 作

如果我们有一个数组,其中包含两次出现的数字,以及一个只出现一次的数字,我们可以使用XOR运算符根据通信定律查找它。e、 g

1 2 3 2 3 
1 ^ 2 ^ 3 ^ 2 ^ 3 = ( 2 ^ 2 ) ^ ( 3 ^ 3 ) ^ 1 = 1

但是我们可以使用位技巧来查找数组中出现一次的数字,而其他数字可以出现n次,n>1吗?

使用位运算符,不可以,除非
n
总是偶数

但是还有很多其他方法可以使用,比如对一次出现的项目进行排序和扫描,或者在输入域受到某种限制的情况下将每个项目分配给bucket

作为第一个例子:

def findASingle(list):
    if list length is zero:
        return nothing
    if list length is one:
        return first item in list
    sort list
    for each item in last other than first and last:
        if item is different to both previous and next item:
            return item
对于第二种情况,假设它仅限于(例如)一位数的非负整数:

def findASingle(list):
    create count[0..9], all set to zero
    for each item in last:
        count[item] = count[item] + 1
    for each index 0 through 9 inclusive:
        if count[index] is 1:
            return index
    return nothing

对于按位运算符,否,除非
n
始终为偶数

但是还有很多其他方法可以使用,比如对一次出现的项目进行排序和扫描,或者在输入域受到某种限制的情况下将每个项目分配给bucket

作为第一个例子:

def findASingle(list):
    if list length is zero:
        return nothing
    if list length is one:
        return first item in list
    sort list
    for each item in last other than first and last:
        if item is different to both previous and next item:
            return item
对于第二种情况,假设它仅限于(例如)一位数的非负整数:

def findASingle(list):
    create count[0..9], all set to zero
    for each item in last:
        count[item] = count[item] + 1
    for each index 0 through 9 inclusive:
        if count[index] is 1:
            return index
    return nothing

有一种方法可以做到这一点,但不能使用二进制运算符。您可以将每个数字表示为位向量,然后使用sum(mod n)将所有向量相加。结果向量将表示该唯一数字

例如,让我们考虑n=3和序列2、3、5、2、5、5、2、

向量是:
[0 1 0]
[0 1 1]
[1 0 1]
[0 1 0]
[1 0 1]
[1 0 1]
[0 1 0]

所有向量的每个元素和为:[
34
]

Mod 3将是:
[0 1]
,相当于序列中的3个唯一元素


这是XOR技巧的推广;事实上,XOR正是这种运算——mod 2中的求和。

有一种方法可以做到这一点,但不能使用二进制运算符。您可以将每个数字表示为位向量,然后使用sum(mod n)将所有向量相加。结果向量将表示该唯一数字

例如,让我们考虑n=3和序列2、3、5、2、5、5、2、

向量是:
[0 1 0]
[0 1 1]
[1 0 1]
[0 1 0]
[1 0 1]
[1 0 1]
[0 1 0]

所有向量的每个元素和为:[
34
]

Mod 3将是:
[0 1]
,相当于序列中的3个唯一元素


这是XOR技巧的推广;事实上,XOR正是这种运算——mod 2中的求和。

相当确定,只有当
n%2==0
@NathanOliver是的,它们只有成对出现时才能相互抵消。这就像是将负数相乘:2、4或6个负数产生一个正数,因为这些符号相互抵消。3、5或7个负数都有一个“悬空”负数到达最终输出。相当确定的是,只有当
n%2==0
@NathanOliver是的,它们成对出现时才相互抵消。这就像是将负数相乘:2、4或6个负数产生一个正数,因为这些符号相互抵消。3、5或7个负数中的每一个都有一个“悬空”负数,该负数指向最终输出。