首页 > 其他分享 >[leetcode每日一题]1.5

[leetcode每日一题]1.5

时间:2023-01-05 14:31:57浏览次数:56  
标签:1.5 XOR nums i32 每日 数对 high low leetcode

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

难度 困难

给你一个整数数组 ​​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

Solution

本来想学一下字典树的,结果发现Rust可以暴力过……编译器优化太强了……

所以摸了。

代码(Rust)

impl Solution {
pub fn count_pairs(nums: Vec<i32>, low: i32, high: i32) -> i32 {
let n = nums.len();
let mut res = 0;
for i in 0..n{
for j in 0..i{
let tmp: i32 = nums[i] ^ nums[j];
if tmp >= low && tmp <= high{
res += 1;
}
}
}
res
}
}

标签:1.5,XOR,nums,i32,每日,数对,high,low,leetcode
From: https://blog.51cto.com/u_15763108/5991091

相关文章

  • LeetCode 5_最长回文子串
    LeetCode5:最长回文子串题目给你一个字符串s,找到s中最长的回文子串。如果字符串的反序与原始字符串相同,则该字符串称为回文字符串。示例1:输入:s="babad"输出:"b......
  • Leetcode 重排链表 递归
    https://leetcode.com/problems/reorder-list/solutions/45113/Share-a-consise-recursive-solution-in-C++/https://leetcode.cn/problems/reorder-list/solutions/32910......
  • 【栈】LeetCode 1209. 删除字符串中的所有相邻重复项 II
    题目链接1209.删除字符串中的所有相邻重复项II思路用栈存储Pair<Character,Integer>,整数表示该字符连续出现的次数。遍历字符串s将其中的字符c依次压入栈顶并......
  • 【栈】LeetCode 1472. 设计浏览器历史记录
    题目链接1472.设计浏览器历史记录思路用栈history模拟网页的前进后退操作,用栈temp来暂时存储后退所退出的网页。代码classBrowserHistory{Stack<String>h......
  • [LeetCode] 2453. Destroy Sequential Targets
    Youaregivena 0-indexed array nums consistingofpositiveintegers,representingtargetsonanumberline.Youarealsogivenaninteger space.Youhave......
  • leetcode-653. 两数之和 IV - 输入二叉搜索树
    653.两数之和IV-输入二叉搜索树-力扣(Leetcode)用了迭代进行遍历二叉树,因为二叉搜索树的中序遍历是有序的,所以肯定左边大于右边,然后就可以用一个map来存放之前的数值,......
  • [LeetCode]015-三数之和
    >>>传送门题目给你一个整数数组nums,判断是否存在三元组[nums[i],nums[j],nums[k]]满足i!=j、i!=k且j!=k,同时还满足nums[i]+nums[j]+nums[k]==0......
  • 20230103_每日学习记录
    20230103做多线程爬虫,需要有些对抗反扒机制的措施.有些时候直接写多线程,比如python的multiprocessing,会发现抓不下来东西.这也可能是我的爬虫没写好.但是就是发现同......
  • leetcode-387-easy
    FirstUniqueCharacterinaStringGivenastrings,findthefirstnon-repeatingcharacterinitandreturnitsindex.Ifitdoesnotexist,return-1.Examp......
  • leetcode-414-easy
    ThirdMaximumNumberGivenanintegerarraynums,returnthethirddistinctmaximumnumberinthisarray.Ifthethirdmaximumdoesnotexist,returnthemaxim......