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:

Input: [0]
Output: 1
Explanation:
There is only one possible result: 0.

Example 2:

Input: [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:

Input: [1,2,4]
Output: 6
Explanation:
The possible results are 1, 2, 3, 4, 6, and 7.

Note:

  1. 1 <= A.length <= 50000
  2. 0 <= A[i] <= 10^9

这是一道蛮有意思的题目,说是给了我们一个数组,里面都是非负数,问我们所有连续的子数组'或'起来能产生多少个不同的值。虽说这只是一道 Medium 的题,但是直觉告诉博主,暴力遍历所有子数组,并且一个一个的'或'将会产生大量的重复运算,不出意外应该是会 TLE 的。所以博主的第一直觉是能不能建立类似累加和一样的数组,然后快速计算任意区间的总'或'值,尝试了一下,虽然可以建立累加'或'数组,但是无法得到正确的区间总'或'值,博主甚至尝试了'异或',仍然不对,只得作罢。其实这道题的正确解法还是蛮巧妙的,感觉不容易一下子想到,这里主要参考了 [网上大神 zhoubowei 的帖子](https://leetcode.com/problems/bitwise-ors-of-subarrays/discuss/165859/C%2B%2B-O(kN)-solution),举个例子吧,比如数组 [0, 3, 4, 6, 5],写成二进制的就是 [001, 011, 100, 110, 101],生成子数组的方法跟生成子集合 [Subsets](http://www.cnblogs.com/grandyang/p/4309345.html) 有些类似,但由于子数组必须是连续的,所以个数比子集合要少一些,生成的方法也是在现有的集合都加入当前数字,并每次新加一个只有当前数字的集合,顺序如下:

[001]
[001 011] [011]
[001 011 100] [011 100] [100]
[001 011 100 110] [011 100 110] [100 110] [110]
[001 011 100 110 101] [011 100 110 101] [100 110 101] [110 101] [101]

我们可以看到,最开始就只有一个集合 [001],然后对于数字 011,先放到现有集合中,变成 [001 011],然后再新建一个自己的集合 [011],对于后面的数字都是同样的操作,最后我们就有5个不同的集合,代表了所有的子数组,我们对每个集合都计算总'或'值,可以得到:

001
011 011
111 111 100
111 111 110 110
111 111 111 111 101

之前提到了,若对于每个集合都一个一个的'或'起来,将会十分的不高效,而其实这里面可能会有许多重复值,所以对重复值只需要保留一个,实际上就可以变成:

001
011
111 100
111 110
111 101

这样数字就减少了很多,使得计算效率也就大大的提高了。具体的做法是,开始先建立两个 HashSet,分别是 res 和 cur,然后遍历数组A,对于每个遍历到的数字,首先生成一个自己的集合 tmp,然后遍历集合 cur 中的所有数字,将当前数字和 cur 中的每个数字相'或',并存入 tmp 中,由于 HashSet 可以自动去重复,所以 tmp 中保存的只有不同的值,然后将 tmp 全部赋值给 cur,再将 cur 中的所有值加入结果 res 中,由于结果 res 也是 HashSet,也可以自动去重复,最后留在 res 中的就是所有不同的子数组的总'或'值,参见代码如下:

class Solution {
public:
int subarrayBitwiseORs(vector<int>& A) {
unordered_set<int> res, cur;
for (int i : A) {
unordered_set<int> tmp = {i};
for (int j : cur) tmp.insert(i | j);
cur = tmp;
for (int j : cur) res.insert(j);
}
return res.size();
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/898

参考资料:

https://leetcode.com/problems/bitwise-ors-of-subarrays/

https://leetcode.com/problems/bitwise-ors-of-subarrays/discuss/165859/C%2B%2B-O(kN)-solution

https://leetcode.com/problems/bitwise-ors-of-subarrays/discuss/165881/C%2B%2BJavaPython-O(30N)

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 898. Bitwise ORs of Subarrays 子数组按位或操作的更多相关文章

  1. [Swift]LeetCode898. 子数组按位或操作 | Bitwise ORs of Subarrays

    We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., ...

  2. 【LeetCode】898. Bitwise ORs of Subarrays 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...

  3. 898. Bitwise ORs of Subarrays

    We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., ...

  4. LC 898. Bitwise ORs of Subarrays

    We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., ...

  5. [LeetCode] Subarray Product Less Than K 子数组乘积小于K

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  6. [LeetCode] 907. Sum of Subarray Minimums 子数组最小值之和

    Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarra ...

  7. [LeetCode] 560. Subarray Sum Equals K 子数组和为K

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  8. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  9. [LeetCode] Continuous Subarray Sum 连续的子数组之和

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

随机推荐

  1. POJ 3041 Asteroids(二分图最大匹配)

    ###题目链接### 题目大意: 给你 N 和 K ,在一个 N * N 个图上有 K 个 小行星.有一个可以横着切或竖着切的武器,问最少切多少次,所有行星都会被毁灭. 分析: 将 1~n 行数加入左 ...

  2. Java-100天知识进阶-GC种类-知识铺(六)

    知识铺: 致力于打造轻知识点,持续更新每次的知识点较少,阅读不累.不占太多时间,不停的来唤醒你记忆深处的知识点. 一.GC回收器的 4个指标: 1.Throughput,非gc时间与总运行时间的比重. ...

  3. 第三节: List类型的介绍、生产者消费者模式、发布订阅模式

    一. List类型基础 1.介绍 它是一个双向链表,支持左进.左出.右进.右出,所以它即可以充当队列使用,也可以充当栈使用. (1). 队列:先进先出, 可以利用List左进右出,或者右进左出(Lis ...

  4. HashHelper

    在C#中,数据的Hash以MD5或SHA-1的方式实现,MD5与SHA1都是Hash算法,MD5输出是128位的,SHA1输出是160位的,MD5比SHA1快,SHA1比MD5强度高. MD5与SHA ...

  5. 对象数组自定义排序--System.Collections.ArrayList.Sort()

    使用System.Collections.ArrayList.Sort()对象数组自定义排序 其核心为比较器的实现,比较器为一个类,继承了IComparer接口并实现int IComparer.Com ...

  6. ASP.NET MVC IOC 之 Autofac 系列开篇

    本系列主要讲述Autofac在.NET MVC项目以及webform中的使用. autofac为IOC组件,实现控制反转,主要结合面向接口编程,完成较大程度的解耦工作. 作为初学者,将学习到的每一步, ...

  7. sess文件编译输出css的四种方式以及使用

    sess文件输出css有下面四种方式: :nested(嵌套) :compact(紧凑) :expanded(展开) :compressed(压缩) 如何使用: sass --watch style. ...

  8. RDD源码分析

    RDD源码解析 一. RDD.scala - Resilient Distributed Dataset (RDD) 弹性分布式数据集 弹性: 体现在计算上面 - the basic abstract ...

  9. tcp_tw_recycle参数引发的数据库连接异常

    [问题描述] 开发反馈有个应用在后端数据库某次计划性重启后经常会出现数据库连接异常问题,通过监控系统的埋点数据,发现应用连接数据库异常有两类表现:   其一:连接超时   131148.00ms To ...

  10. 3-2-Pandas 索引

    Pandas章节应用的数据可以在以下链接下载:  https://files.cnblogs.com/files/AI-robort/Titanic_Data-master.zip In [4]: i ...