题目
We have an array A of non-negative integers.
For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j].
Return the number of possible results. (Results that occur more than once are only counted once in the final answer.)
答案
class Solution {
public int subarrayBitwiseORs(int[] A) {
Set<Integer> result_set = new HashSet<>();
Set<Integer> lastline_set = new HashSet<>();
for(int a : A) {
Set<Integer> currline_set = new HashSet<>();
currline_set.add(a);
for(int s : lastline_set) {
currline_set.add(s | a);
}
for(int s : currline_set) {
result_set.add(s);
}
lastline_set = currline_set;
}
return result_set.size();
}
}
网友评论