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

Github 同步地址:

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

参考资料:

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

LeetCode All in One 题目讲解汇总(持续更新中...)

原文地址:https://www.cnblogs.com/grandyang/p/10982534.html

时间: 2024-11-01 01:24:26

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

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 re

[LeetCode]560. 和为K的子数组(前缀和)

题目 给定一个整数数组和一个整数?k,你需要找到该数组中和为?k?的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1,1] 为两种不同的情况. 说明 : 数组的长度为 [1, 20,000]. 数组中元素的范围是 [-1000, 1000] ,且整数?k?的范围是?[-1e7, 1e7]. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/subarray-sum-eq

LeetCode OJ:Maximum Subarray(子数组最大值)

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6. 典型的DP问题,递推条件还是想了有点长时间,代码如下所示: 1

子序列的按位或 Bitwise ORs of Subarrays

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

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

Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead. Example: Input: s = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: the subarr

[Swift]LeetCode1031. 两个非重叠子数组的最大和 | Maximum Sum of Two Non-Overlapping Subarrays

Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L and M.  (For clarification, the L-length subarray could occur before or after the M-length subarray.) Fo

【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 

LeetCode 907 子数组的最小值之和

给定一个整数数组 A,找到 min(B) 的总和,其中 B 的范围为 A 的每个(连续)子数组.由于答案可能很大,因此返回答案模 10^9 + 7. 示例: 输入:[3,1,2,4] 输出:17 解释: 子数组为 [3],[1],[2],[4],[3,1],[1,2],[2,4],[3,1,2],[1,2,4],[3,1,2,4]. 最小值为 3,1,2,4,1,1,2,1,1,1,和为 17. 提示: 1 <= A <= 30000 1 <= A[i] <= 30000 原文地址

Leetcode 643.子数组最大平均数I

子数组最大平均数I 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. 示例 1: 输入: [1,12,-5,-6,50,3], k = 4 输出: 12.75 解释: 最大平均数 (12-5-6+50)/4 = 51/4 = 12.75 注意: 1 <= k <= n <= 30,000. 所给数据范围 [-10,000,10,000]. 思路 本题拿到手的时候,第一个思路就是对每一个元素都去求其长度为k的连续子数组的平均数,最后比较返回最大平均数,这样的