网易笔试第二题 OR | 与
第二题题目写的是按位与,文字中给的时OR |,说的又是与
测试用例等于白给,查询为YES的都是插入的原值
我按照按位与来搞的,最后还是10%
按位与:
```
public static boolean contain(Set<Integer> set, int target) {
if (set.contains(target))
return true;
Set<Integer> select = new HashSet<>(set);
for (int i = 0; i < 32; i++) {
//target对应位为1 目标值全为1才行
if (bitofIndex(target,i) != 0) {
Set<Integer> tmp = new HashSet<>();
for (int num : select) {
if (bitofIndex(num, i) != 0) {
tmp.add(num);
}
}
select = tmp;
if (select.isEmpty())
break;
}
}
int end = 0xffffffff;
for (int num : select){
end = end & num;
}
return end == target;
}
static int bitofIndex(int a, int index) {
return a & (1 << index);
}
if (set.contains(target))
return true;
Set<Integer> select = new HashSet<>(set);
for (int i = 0; i < 32; i++) {
//target对应位为1 目标值全为1才行
if (bitofIndex(target,i) != 0) {
Set<Integer> tmp = new HashSet<>();
for (int num : select) {
if (bitofIndex(num, i) != 0) {
tmp.add(num);
}
}
select = tmp;
if (select.isEmpty())
break;
}
}
int end = 0xffffffff;
for (int num : select){
end = end & num;
}
return end == target;
}
static int bitofIndex(int a, int index) {
return a & (1 << index);
}
```
按位或思路是类似的,如果对应位为0,集合中所有位都为0才行,最后候选集合都按位或,target为1的位上只要有存在1的,得到的结果就和target相同
现在问题是当时应该也不是眼瞎了呀,题目很矛盾,我按照与来做了,结果应该是按位或来判断的,只过了直接判断集合的10%,伤心
#网易##笔试题目#