1803:统计异或值在范围内的数对有多少

This commit is contained in:
huangge1199 2021-06-02 16:01:32 +08:00
parent 7537b0b5f3
commit c51e9c0b03
3 changed files with 126 additions and 1 deletions

View File

@ -0,0 +1,82 @@
//给你一个整数数组 nums 下标 0 开始 计数以及两个整数low high 请返回 漂亮数对 的数目
//
// 漂亮数对 是一个形如 (i, j) 的数对其中 0 <= i < j < nums.length low <= (nums[i] XOR nums[
//j]) <= high
//
//
//
// 示例 1
//
// 输入nums = [1,4,2,7], low = 2, high = 6
//输出6
//解释所有漂亮数对 (i, j) 列出如下
// - (0, 1): nums[0] XOR nums[1] = 5
// - (0, 2): nums[0] XOR nums[2] = 3
// - (0, 3): nums[0] XOR nums[3] = 6
// - (1, 2): nums[1] XOR nums[2] = 6
// - (1, 3): nums[1] XOR nums[3] = 3
// - (2, 3): nums[2] XOR nums[3] = 5
//
//
// 示例 2
//
// 输入nums = [9,8,4,2,1], low = 5, high = 14
//输出8
//解释所有漂亮数对 (i, j) 列出如下
// - (0, 2): nums[0] XOR nums[2] = 13
//  - (0, 3): nums[0] XOR nums[3] = 11
//  - (0, 4): nums[0] XOR nums[4] = 8
//  - (1, 2): nums[1] XOR nums[2] = 12
//  - (1, 3): nums[1] XOR nums[3] = 10
//  - (1, 4): nums[1] XOR nums[4] = 9
//  - (2, 3): nums[2] XOR nums[3] = 6
//  - (2, 4): nums[2] XOR nums[4] = 5
//
//
//
// 提示
//
//
// 1 <= nums.length <= 2 * 104
// 1 <= nums[i] <= 2 * 104
// 1 <= low <= high <= 2 * 104
//
// Related Topics 字典树
// 👍 40 👎 0
package leetcode.editor.cn;
//1803:统计异或值在范围内的数对有多少
public class CountPairsWithXorInARange {
public static void main(String[] args) {
//测试代码
Solution solution = new CountPairsWithXorInARange().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int countPairs(int[] nums, int low, int high) {
int[] xors = new int[32768];
int count = 0;
for (int i = 1; i < nums.length; i++) {
int num = nums[0] ^ nums[i];
if (num >= low && num <= high) {
count++;
}
xors[num]++;
}
for (int i = 1; i < nums.length; i++) {
int xor = nums[0] ^ nums[i];
xors[xor]--;
for (int j = low; j <= high; j++) {
int index = xor ^ j;
count += xors[index];
}
}
return count;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,43 @@
<p>给你一个整数数组 <code>nums</code> (下标 <strong>从 0 开始</strong> 计数)以及两个整数:<code>low</code><code>high</code> ,请返回 <strong>漂亮数对</strong> 的数目。</p>
<p><strong>漂亮数对</strong> 是一个形如 <code>(i, j)</code> 的数对,其中 <code>0 &lt;= i &lt; j &lt; nums.length</code><code>low &lt;= (nums[i] XOR nums[j]) &lt;= high</code></p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>nums = [1,4,2,7], low = 2, high = 6
<strong>输出:</strong>6
<strong>解释:</strong>所有漂亮数对 (i, j) 列出如下:
- (0, 1): nums[0] XOR nums[1] = 5
- (0, 2): nums[0] XOR nums[2] = 3
- (0, 3): nums[0] XOR nums[3] = 6
- (1, 2): nums[1] XOR nums[2] = 6
- (1, 3): nums[1] XOR nums[3] = 3
- (2, 3): nums[2] XOR nums[3] = 5
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>nums = [9,8,4,2,1], low = 5, high = 14
<strong>输出:</strong>8
<strong>解释:</strong>所有漂亮数对 (i, j) 列出如下:
- (0, 2): nums[0] XOR nums[2] = 13
  - (0, 3): nums[0] XOR nums[3] = 11
  - (0, 4): nums[0] XOR nums[4] = 8
  - (1, 2): nums[1] XOR nums[2] = 12
  - (1, 3): nums[1] XOR nums[3] = 10
  - (1, 4): nums[1] XOR nums[4] = 9
  - (2, 3): nums[2] XOR nums[3] = 6
  - (2, 4): nums[2] XOR nums[4] = 5</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li>
<li><code>1 &lt;= low &lt;= high &lt;= 2 * 10<sup>4</sup></code></li>
</ul>
<div><div>Related Topics</div><div><li>字典树</li></div></div>\n<div><li>👍 40</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long