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

Approach #1: Brute force. [C++] [TEL]

    int subarrayBitwiseORs1(vector<int>& A) {
int len = A.size();
set<int> ans;
for (int i = 0; i < len; ++i) {
for (int j = i; j < len; ++j) {
int temp = 0;
for (int k = i; k <= j; ++k) {
temp |= A[k];
}
ans.insert(temp);
}
} return ans.size();
}

  

Approach #2: DP[ ][ ]. [C++] [TEL]

    int subarrayBitwiseORs2(vector<int>& A) {
int len = A.size();
unordered_set<int> ans(begin(A), end(A));
vector<vector<int>> dp(len, vector<int>(len)); for (int l = 1; l <= len; ++l) {
for (int i = 0; i <= len - l; ++i) {
int j = i + l - 1;
if (l == 1) {
dp[i][j] = A[j];
continue;
} dp[i][j] = dp[i][j-1] | A[j];
ans.insert(dp[i][j]);
}
} return ans.size();
}

  

Approach #3: DP[ ]. [C++] [TEL]

    int subarrayBitwiseORs3(vector<int>& A) {
int len = A.size();
unordered_set<int> ans(begin(A), end(A));
vector<int> dp(A); for (int l = 2; l <= len; ++l) {
for (int i = 0; i <= len - l; ++i) {
ans.insert(dp[i] |= A[i+l-1]);
}
} return ans.size();
}

  

dp[i][j] = dp[i] | dp[i+1] | ..... | dp[j]

dp[i][j] = dp[i][j-1] | A[j]

ans = len(set(dp))

Time complexity: O(n^2)

Space complexity: O(n^2) -> O(n)

Approach #4: DP + Bit. [C++]

    int subarrayBitwiseORs(vector<int>& A) {
unordered_set<int> ans;
unordered_set<int> cur;
unordered_set<int> nxt; for (int a : A) {
nxt.clear();
nxt.insert(a);
for (int c : cur) {
nxt.insert(c | a);
}
cur.swap(nxt);
ans.insert(begin(cur), end(cur));
} return ans.size();
}

  

Approach #5: DP + Bit. [Java]

    public int subarrayBitwiseORs(int[] A) {
Set<Integer> ans = new HashSet<>();
Set<Integer> cur = new HashSet<>(); for (int a : A) {
Set<Integer> nxt = new HashSet<>();
nxt.add(a);
for (int b : cur) {
nxt.add(b | a);
}
ans.addAll(nxt);
cur = nxt;
} return ans.size();
}

  

Approach #6: DP + Bit. [Python]

class Solution(object):
def subarrayBitwiseORs(self, A):
"""
:type A: List[int]
:rtype: int
"""
cur = set()
ans = set() for a in A:
cur = {a | b for b in cur} | {a}
ans |= cur return len(ans)

  

Analysis:

Reference:

https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-898-bitwise-ors-of-subarrays/

898. Bitwise ORs of Subarrays的更多相关文章

  1. [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], ..., ...

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

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

  3. 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], ..., ...

  4. [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], ..., ...

  5. 子序列的按位或 Bitwise ORs of Subarrays

    2018-09-23 19:05:20 问题描述: 问题求解: 显然的是暴力的遍历所有的区间是不可取的,因为这样的时间复杂度为n^2级别的,对于规模在50000左右的输入会TLE. 然而,最后的解答也 ...

  6. LeetCode编程训练 - 位运算(Bit Manipulation)

    位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...

  7. 算法与数据结构基础 - 位运算(Bit Manipulation)

    位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...

  8. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. Phalcon Framework的MVC结构及启动流程分析

    目前的项目中选择了Phalcon Framework作为未来一段时间的核心框架.技术选型的原因会单开一篇Blog另说,本次优先对Phalcon的MVC架构与启动流程进行分析说明,如有遗漏还望指出. P ...

  2. windows,phalcon工具的安装使用

    一.使用工具之前,必须安装phalcon的扩展,也就是php_phalcon.dll动态链接库 phalcon官方地址:https://github.com/phalcon/cphalcon/rele ...

  3. 原生JS获取url汇总

    在WEB开发中,许多开发者都比较喜欢使用javascript来获取当前url网址,本文就此为大家总结一下比较常用获取URL的javascript实现代码 URL即统一资源定位符 (Uniform Re ...

  4. ObjC.instancetype

    1. instancetype http://nshipster.com/instancetype/ 2. Objc的扩展 http://clang.llvm.org/docs/LanguageExt ...

  5. 记unit of work与事务提交

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using- ...

  6. centos / debian 安装iptables服务

    debian: #使用可参考 https://www.debian.cn/archives/991 #配置文件位于 /etc/iptables #重新配置使用dpkg-reconfigure ipta ...

  7. 2018.10.22 bzoj1009: [HNOI2008]GT考试(kmp+矩阵快速幂优化dp)

    传送门 f[i][j]f[i][j]f[i][j]表示从状态"匹配了前i位"转移到"匹配了前j位"的方案数. 这个东西单次是可以通过跳kmp的fail数组得到的 ...

  8. Django入门与实践-第23章:分页实现(完结)

    http://127.0.0.1:8000/boards/1/ #从现在起,我们将在 board_topics 这个视图中来操作. python manage.py shell from django ...

  9. java常用设计模式一:单例模式

     1.饿汉式 package singleton.demo; /** * @author Administrator * @date 2019/01/07 */ public class Single ...

  10. js splice方法

    处理数组的方法很多,javascript splice()算是最强大的了,它可以用于插入.删除或替换数组的元素.下面来一一介绍! 1.删除-用于删除元素,两个参数,第一个参数(要删除第一项的位置),第 ...