LeetCode每日一题(898. Bitwise ORs of Subarrays)

假设 curr[i]是所有以 arr[i]为终点的结果集, 那 curr[i+1]就应该是 curr[i] | arr[i+1], 这个不难想出来, 难想的是这个 curr 的 size 最大也就是 32 个, 因为我们做的是|操作, 从数值的角度来看, 一定是单调递增的, 而这种总递增还是以 32 位中的 1 位或者多位从 0 变成 1 来进行递增, 所以最多也就 32 个数。能想明白这个,那这个题就不难解了, 本来认为不可行的暴力解法现在变得可行了。...
摘要由CSDN通过智能技术生成

We have an array arr of non-negative integers.

For every (contiguous) subarray sub = [arr[i], arr[i + 1], …, arr[j]] (with i <= j), we take the bitwise OR of all the elements in sub, obtaining a result arr[i] | arr[i + 1] | … | arr[j].

Return the number of possible results. Results that occur more than once are only counted once in the final answer

Example 1:

Input: arr = [0]
Output: 1

Explanation: There is only one possible result: 0.

Example 2:

Input: arr = [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: arr = [1,2,4]
Output: 6

Explanation: The possible results are 1, 2, 3, 4, 6, and 7.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值