LeetCode第277场周赛-2022/1/23

元素计数

题目描述
元素计数
给你一个整数数组 nums ,统计并返回在 nums 中同时具有一个严格较小元素和一个严格较大元素的元素数目。

  • 1 <= nums.length <= 100
  • − 1 0 5 -10^5 105 <= nums[ i ] <= 1 0 5 10^5 105

输入:nums = [-3,3,3,90]
输出:2

题目分析
排序后去掉首尾。


class Solution {
public:
    int countElements(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int ans = nums.size();
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] == nums[0] || nums[i] == nums[nums.size() - 1]) {
                ans--;
            }
        }
        return ans;
    }
};

按符号重排数组

题目描述
按符号重排数组
给你一个下标从 0 开始的整数数组 nums ,数组长度为偶数,由数目相等的正整数和负整数组成。你需要重排 nums 中的元素,使修改后的数组满足下述条件:
任意连续的两个整数符号相反
对于符号相同的所有整数,保留它们在 nums 中的顺序。
重排后数组以正整数开头。
重排元素满足上述条件后,返回修改后的数组。

  • 2 <= nums.length <= 2 * 1 0 5 10^5 105
  • nums.length 是偶数
  • 1 <= |nums[ i ]| <= 1 0 5 10^5 105
  • nums 由相等数量的正整数和负整数组成

输入:nums = [3,1,-2,-5,2,-4]
输出:[3,-2,1,-5,2,-4]

题目分析
分组。


class Solution {
public:
    vector<int> rearrangeArray(vector<int>& nums) {
        int i = 0, j = 1, n = nums.size();
        vector<int> ans(n);
        for (auto each: nums) {
            if (each > 0) {
                ans[i] = each;
                i += 2;
            }
            else {
                ans[j] = each;
                j += 2;
            }
        }
        return ans;
    }
};

找出数组中的所有孤独数字

题目描述
找出数组中的所有孤独数字
给你一个整数数组 nums 。如果数字 x 在数组中仅出现一次,且没有相邻数字(即,x + 1 和 x - 1)出现在数组中,则认为数字 x 是孤独数字。
返回 nums 中的所有孤独数字。你可以按任何顺序返回答案。

  • 1 <= nums.length <= 1 0 5 10^5 105
  • 0 <= nums[ i ] <= 1 0 6 10^6 106

输入:nums = [1,3,5,3]
输出:[1,5]

题目分析
排序简单模拟,注意处理边界 o r or or 哈希表。


方法一:排序+简单模拟

class Solution {
public:
    vector<int> findLonely(vector<int>& nums) {
        vector<int> ans;
        int n = nums.size();
        sort(nums.begin(), nums.end());
        if (n == 1) {
            ans.push_back(nums[0]);
            return ans;
        }
        for (int i = 1; i < n - 1; i++) {
            if (nums[i + 1] != nums[i] && nums[i + 1] != nums[i] + 1 && nums[i - 1] != nums[i] && nums[i - 1] != nums[i] - 1) {
                ans.push_back(nums[i]);
            }
        }
        if (nums[1] != nums[0] && nums[1] != nums[0] + 1) {
            ans.push_back(nums[0]);
        }
        if (nums[n - 1] != nums[n - 2] && nums[n - 1] != nums[n - 2] + 1) {
            ans.push_back(nums[n - 1]);
        }
        return ans;
    }
};

方法二:哈希表

class Solution {
public:
    vector<int> findLonely(vector<int>& nums) {
        unordered_map<int, int> count;
        vector<int> ans;
        for (auto each: nums) {
            count[each]++;
        }
        for (auto each: nums) {
            if (count[each - 1] == 0 && count[each + 1] == 0 && count[each] == 1) {
                ans.push_back(each);
            }
        }
        return ans;
    }
};

基于陈述统计最多好人数

题目描述
基于陈述统计最多好人数
游戏中存在两种角色:
好人:该角色只说真话。
坏人:该角色可能说真话,也可能说假话。
给你一个下标从 0 开始的二维整数数组 statements ,大小为 n x n ,表示 n 个玩家对彼此角色的陈述。具体来说,statements[ i ][ j ] 可以是下述值之一:
0 表示 i 的陈述认为 j 是坏人。
1 表示 i 的陈述认为 j 是好人。
2 表示 i 没有对 j 作出陈述。
另外,玩家不会对自己进行陈述。形式上,对所有 0 <= i < n ,都有 statements[ i ][ i ] = 2 。
根据这 n 个玩家的陈述,返回可以认为是好人的最大数目。

  • n == statements.length == statements[ i ].length
  • 2 <= n <= 15
  • statements[ i ][ j ] 的值为 0、1 或 2
  • statements[ i ][ i ] == 2

输入:statements = [[2,1,2],[1,2,2],[2,0,2]]
输出:2

题目分析
由于 2 ≤ n ≤ 15 2\le n\le 15 2n15,可直接枚举,考虑使用二进制表示,第 i i i 位(从低位到高位)为 1 1 1 表示 i i i 为好人, 0 0 0 表示坏人,如 1101 1101 1101 0 , 2 , 3 0,2,3 0,2,3 是好人, 1 1 1 是坏人。那么 [ 0 , 2 n − 1 ] [0,2^n-1] [0,2n1] 中每一个数的二进制形式都表示一组结果,根据 s t a t e m e n t s statements statements 数组判断每一组结果是否合理,对合理的结果求出其 1 1 1 的总个数(好人数量,在C++中可用 _ _ b u i l t i n _ p o p c o u n t \_\_ builtin\_ popcount __builtin_popcount 函数统计二进制数中 1 1 1 的数量),最多的即为答案。

  • s t a t e m e n t s [ i ] [ j ] = = 0 statements[i][j]==0 statements[i][j]==0 i i i 认为 j j j 是坏人,若 j j j 是好人且 i i i 也是好人那么便不合理;
  • s t a t e m e n t s [ i ] [ j ] = = 1 statements[i][j]==1 statements[i][j]==1 i i i 认为 j j j 是好人,若 j j j 是坏人但 i i i 是好人那么便不合理;
  • s t a t e m e n t s [ i ] [ j ] = = 2 statements[i][j]==2 statements[i][j]==2 时不用判断。

class Solution {
public:
    int maximumGood(vector<vector<int>>& statements) {
        int n = statements.size(), ans = 0;
        auto check = [&](int x)->bool {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (statements[i][j] == 0) {//i认为j是坏人
                        if ((x & (1 << i)) && (x & (1 << j))) {//i是好人,j是好人
                            return false;
                        }
                    }
                    else if (statements[i][j] == 1) {//i认为j是好人
                        if ((x & (1 << i)) && !(x & (1 << j))) {//i是好人,j是坏人
                            return false;
                        }
                    }
                }
            }
            return true;
        };
        for (int trans = 0; trans < (1 << n); trans++) {
            if (check(trans)) {
                ans = max(ans, __builtin_popcount(trans));
            }
        }
        return ans;
    }
};
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值