leetcode在力扣 App 中打开
调试中...
调试中...
题目描述
题目描述
题解
题解
提交记录
提交记录
代码
代码
测试用例
测试用例
测试结果
测试结果
困难
相关标签
相关企业
提示

给你一个整数数组 nums 和一个整数 k ,请你返回 nums 中有多少个满足:子数组中所有元素按位 AND 的结果为 k 。

 

示例 1:

输入:nums = [1,1,1], k = 1

输出:6

解释:

所有子数组都只含有元素 1 。

示例 2:

输入:nums = [1,1,2], k = 1

输出:3

解释:

按位 AND 值为 1 的子数组包括:[1,1,2], [1,1,2], [1,1,2] 。

示例 3:

输入:nums = [1,2,3], k = 2

输出:2

解释:

按位 AND 值为 2 的子数组包括:[1,2,3], [1,2,3] 。

 

提示:

  • 1 <= nums.length <= 105
  • 0 <= nums[i], k <= 109
通过次数
3K
提交次数
8.6K
通过率
34.8%


相关企业

提示 1
Let’s say we want to count the number of pairs (l, r) such that nums[l] & nums[l + 1] & … & nums[r] == k.

提示 2
Fix the left index l.

提示 3
Note that if you increase r for a fixed l, then the AND value of the subarray either decreases or remains unchanged.

提示 4
Therefore, consider using binary search.

提示 5
To calculate the AND value of a subarray, use sparse tables.

评论 (0)

贡献者
© 2025 领扣网络(上海)有限公司
0 人在线
行 1,列 1
nums =
[1,1,1]
k =
1
Source