898. Bitwise ORs of Subarrays
Solution
題目類似 467. Unique Substrings in Wraparound String
但因為integer之間的or不像abcd有連續的特質,不能用某一個性質去判斷所有的可能性
比較直截的做法就是HashMap + brute force
public int subarrayBitwiseORs(int[] A) {
Set<Integer> ans = new HashSet<>();
Set<Integer> prev = new HashSet<>();
Set<Integer> cur;
for(int num : A){
cur = new HashSet<>();
prev.add(0);
for(int p : prev){
cur.add(p|num);
ans.add(p|num);
}
prev = cur;
}
return ans.size();
}
Last updated
Was this helpful?