leetcode-898-Bitwise ORs of Subarrays
问题
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.)
Example 1:
1 | Input: [0] |
Example 2:
1 | Input: [1,1,2] |
Example 3:
1 | Input: [1,2,4] |
Note:
1 <= A.length <= 50000
0 <= A[i] <= 10^9
分析
遍历输入数组,对于每一个 a[i]
,考虑以 a[i]
为末尾的所有子数字情况,则有 a[i]
自己独立为一个字数组,或者a[i]
加入到每一个以a[i-1]
,为结尾的字数组中。因为覆盖关系,每次操作最多只会处理30个数。因此,时间复杂度是 $O(30N)$。https://leetcode.com/problems/bitwise-ors-of-subarrays/discuss/165881/C%2B%2BJavaPython-O(30N)
代码
1 | class Solution { |
Author: Hatton.Liu
Link: http://hattonl.github.io/2020/03/29/leetcode-898/
License: 知识共享署名-非商业性使用 4.0 国际许可协议