898. Bitwise ORs of Subarrays
Given an integer array arr, return the number of distinct bitwise ORs of all the non-empty subarrays of arr.
The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise OR of a subarray of one integer is that integer.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Example 2:
Input: arr = [1,1,2]
Output: 3
Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2].
These yield the results 1, 1, 2, 1, 3, 3.
There are 3 unique values, so the answer is 3.
Example 3:
Constraints:
1 <= arr.length <= 5 * 1040 <= arr[i] <= 109
Solution:
题看不懂系列
class Solution {
public int subarrayBitwiseORs(int[] arr) {
Set<Integer> result = new HashSet<>();
Set<Integer> cur = new HashSet<>();
for (int x : arr){
Set<Integer> next = new HashSet<>();
next.add(x);
for (int v : cur){
next.add(v | x);
}
cur = next;
result.addAll(cur);
}
return result.size();
}
}
// TC: O(n)
// SC: O(n)