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

Runtime: 652 ms

Memory Usage: 49.7 MB

class Solution {
public:
  int subarrayBitwiseORs(vector<int>& A) {
    unordered_set<int> s;
    set<int> t;
    for(int i : A) {
      set<int> r;
      r.insert(i);
      for(int j : t) r.insert(j | i);
      t = r;
      for(int j : t) s.insert(j);
    }
    return s.size();
  }
};

原文地址:https://www.cnblogs.com/ethanhong/p/10352715.html

时间: 2024-08-30 18:33:14

LC 898. Bitwise ORs of Subarrays的相关文章

[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?re

子序列的按位或 Bitwise ORs of Subarrays

2018-09-23 19:05:20 问题描述: 问题求解: 显然的是暴力的遍历所有的区间是不可取的,因为这样的时间复杂度为n^2级别的,对于规模在50000左右的输入会TLE. 然而,最后的解答也可以看作是一个暴力求解,也就是用Set来保存以当前数为结尾的左右可能解,在下一轮中遍历上一轮的所有解并进行或操作. 这里有个难以一下想到的地方就是,乍一看,这个时间复杂度依然是平方级别的,但是实际上,这里的时间复杂度是n级别的,因为Set中后一个数中的1完全覆盖前一个数,因此,最多只有不超过30个数

【LeetCode】位运算 bit manipulation(共32题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [78]Subsets [136]Single Number [137]Single Number II [169]Majority Element [187]Repeated DNA Sequences [190]Reverse Bits [191]Number of 1 Bits [201]Bitwise AND of Numbers Range [231]Pow

前端 crypto-js aes 加解密

https://www.jianshu.com/p/a47477e8126a crypto-js.js ;(function (root, factory) { if (typeof exports === "object") { // CommonJS module.exports = exports = factory(); } else if (typeof define === "function" && define.amd) { // A

LC 992. Subarrays with K Different Integers

Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K. (For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.) Return the nu

Educational Codeforces Round 12 E. Beautiful Subarrays trie求两异或值大于等于k对数

E. Beautiful Subarrays One day, ZS the Coder wrote down an array of integers a with elements a1,  a2,  ...,  an. A subarray of the array a is a sequence al,  al  +  1,  ...,  ar for some integers (l,  r) such that 1  ≤  l  ≤  r  ≤  n. ZS the Coder th

689. Maximum Sum of 3 Non-Overlapping Subarrays三个不重合数组的求和最大值

[抄题]: In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. Each subarray will be of size k, and we want to maximize the sum of all 3*k entries. Return the result as a list of indices representing the star

Lc.exe已退出 代码为-1

今天使用vs2010开发,有人在vss项目中增加了一个第三方组件,后来删除了,我的计算机上没有这个第三方组件,结果导致了LC.exe错误:"Lc.exe已退出 代码为-1 " 解决方法: 1.把项目文件夹下Properties文件夹下的licenses.licx文件删除,重新编译即可: 2.文本方式打开*.csproj文件,在文件中查找licenses.licx字样,删除对应节点. 注意:还有一种情况就是Properties文件夹下已经没有licenses.licx文件了,程序还是报这

chkrootkit 编译报错的解决/usr/bin/ld: cannot find -lc

1:Centos6.5安装chkrootkit wget ftp://ftp.pangeia.com.br/pub/seg/pac/chkrootkit.tar.gz tar xvf chkrootkit.tar.gz cd chkrootkit-0.51/ make sense报错如下: /usr/bin/ld: cannot find -lc collect2: ld returned 1 exit status make: *** [strings-static] Error 1 2:解决