0
点赞
收藏
分享

微信扫一扫

位运算,LeetCode 2917. 找出数组中的 K-or 值

一、题目

1、题目描述

2、接口描述

class Solution {
public:
    int findKOr(vector<int>& nums, int k) {

    }
};

3、原题链接

2917. 找出数组中的 K-or 值


二、解题报告

1、思路分析

统计每一位的1的数目,按要求置位即可

2、复杂度

3、代码详解

class Solution {
public:
    int findKOr(vector<int>& nums, int k) {
        int ret = 0;
        for(int i = 0, cnt = 0; i < 31; i++) {
            cnt = 0;
            for(auto x : nums) cnt += ((x >> i) & 1);
            ret |= ((cnt >= k) << i);
        }
        return ret;
    }
};
举报

相关推荐

0 条评论