Skip to content

Latest commit

 

History

History
45 lines (35 loc) · 1.42 KB

File metadata and controls

45 lines (35 loc) · 1.42 KB

2568. 最小无法得到的或值

给你一个下标从 0 开始的整数数组 nums

如果存在一些整数满足 0 <= index1 < index2 < ... < indexk < nums.length ,得到 nums[index1] | nums[index2] | ... | nums[indexk] = x ,那么我们说 x 是 可表达的 。换言之,如果一个整数能由 nums 的某个子序列的或运算得到,那么它就是可表达的。

请你返回 nums 不可表达的 最小非零整数 。

示例 1:

输入: nums = [2,1]
输出: 4
解释: 1 和 2 已经在数组中,因为 nums[0] | nums[1] = 2 | 1 = 3 ,所以 3 是可表达的。由于 4 是不可表达的,所以我们返回 4 。

示例 2:

输入: nums = [5,3,2]
输出: 1
解释: 1 是最小不可表达的数字。

提示:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109

题解 (Rust)

1. 题解

use std::collections::HashSet;

impl Solution {
    pub fn min_impossible_or(nums: Vec<i32>) -> i32 {
        let nums = nums.into_iter().collect::<HashSet<_>>();

        for i in 0..32 {
            if !nums.contains(&(1 << i)) {
                return 1 << i;
            }
        }

        unreachable!()
    }
}