leetcode898.BitwiseORsofSubarrays
参考:https://blog.csdn.net/u014110320/article/details/82316690
参考:https://blog.csdn.net/yeshen4328/article/details/82563040
题目链接
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.)
Example1
Input: [0]
Output: 1
Explanation:
There is only one possible result: 0.
Example2
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.
Example3
Input: [1,2,4]
Output: 6
Explanation:
The possible results are 1, 2, 3, 4, 6, and 7.
暴力方法
class Solution {public int subarrayBitwiseORs(int[] A) {if(A == null || A.length == 0) {return 0;}Set<Integer> set = new HashSet<>();int max = 0;for(int i = 0; i < A.length; i++) {set.add(A[i]);max |= A[i];set.add(max);}for(int i = 0; i < A.length; i++) {for(int j = i+1, n = 0; j < A.length; j++) {n |= A[j];set.add(n);if(n == max) {break;}}} return set.size();}
}
我们可以首先找出以Ai结尾的所有字串,每一个子串都可以计算一个结果(可能有些是重复的),因为Ai是4字节整数,有32位,而且这一行结果中,左边的结果比右边结果的二进制多一个“1”或者相等(因为左边结果比右边结果多“或”了一个或者多个元素),如果每一个结果都是不一样的,那么这一行结果最大可能有32种:
从A1开始,初始化一个临时set, 将Ai加入到set中,并用Ai和上一个临时set的所有元素做或运算,把结果存进全局set以及当前临时set中,然后进入下一个迭代。示意图如下:
class Solution {public int subarrayBitwiseORs(int[] A) {Set<Integer> set = new HashSet<Integer>() ;Set<Integer> last = new HashSet<Integer>() ;int len = A.length ;for(int i=0;i<len;i++){Set<Integer> temp = new HashSet<Integer>() ;temp.add(A[i]) ;for(int element : last){temp.add(A[i] | element) ;}last = temp ;for(int element :temp){set.add(element) ;}}return set.size() ;}
}
leetcode898.BitwiseORsofSubarrays相关推荐
- Acwing LeetCode 题目分类——配套基础课进阶课
LeetCode 题目分类--配套基础课进阶课 1.基础 二分(满足一个条件的最值问题) LeetCode33 https://leetcode.com/problems/search-in-rota ...
- leetcode算法题--子数组按位或操作
原题链接:https://leetcode-cn.com/problems/bitwise-ors-of-subarrays/ 动态规划 dp[i]表示A[:i]的所有子数组按位或的结果 状态转移 d ...
- LeetCode 898. 子数组按位或操作(前缀和思想)
文章目录 1. 题目 2. 解题 2.1 超时解 2.2 正解 1. 题目 我们有一个非负整数数组 A. 对于每个(连续的)子数组 B = [A[i], A[i+1], ..., A[j]] ( i ...
- leetcode - 898. 子数组按位或操作(使用了集合)
我们有一个非负整数数组 A. 对于每个(连续的)子数组B=[A[i],A[i+1],...,A[j]](i<=j)B = [A[i], A[i+1], ..., A[j]] ( i <= ...
- 清华大学尹成 怎么样
清华大学尹成 怎么样 编程能力极强,算法能力极强,业界的最强算法王者 ============================================================= 清华 ...
- 动态规划(持续更新、整理)
动态规划 记忆化搜索 不同路径:leetcode-62 分割回文串:leetcode-131 单词拆分II:leetcode-140 戳气球:leetcode-312 零钱兑换:leetcode-32 ...
- linux 查看进程阻塞,linux进程 阻塞和非阻塞操作
在我们看全功能的 read 和 write 方法的实现之前, 我们触及的最后一点是决定何时使 进程睡眠. 有时实现正确的 unix 语义要求一个操作不阻塞, 即便它不能完全地进行下去. 有时还有调用进 ...
最新文章
- 卡尔曼滤波— Constant Velocity Model
- IT草根的江湖之路之六:曙光,第一次破例
- 凌乱的yyy(洛谷-P1803)
- kali的burpsuite笔记
- javascript arguments对象研究--针对jquery源码研究再研究
- 剑指Offer面试题:28.连续子数组的最大和
- scp实现mac与linux服务器之间文件传输
- 铁大Facebook——十天冲刺(4)
- Django之路——4 Django的视图层
- 计算机本地配置文件丢失,电脑C盘文件丢失的常见原因及恢复方法
- swift 极光推送
- html取消父元素样式,CSS以防止子元素继承父样式
- Word2016删除插入尾注参考文献下横线
- unity篇-官方序列化接口 ISerializationCallbackReceiver
- win10系统改服务器系统,win10修改更新服务器地址
- CuraEngine切片类详解(转载)
- android穿山甲主题冲突,Flutter 接头条穿山甲广告 Android 总述篇
- 微信小程序:二开版优化新紫色UI云开发新款壁纸小程序源码
- 微信公众平台微信支付打通流程
- Linux MMC子系统分析(二)——Host分析