【今日作业】《七月集训》(第07天)——哈希表(自闭,先做两题)

本文详细介绍了三个算法问题的解决方案:970.强整数,通过遍历并计算x和y的幂次组合找到所有强整数;914.卡牌分组,通过计算所有卡片数量的最大公约数判断是否能分成相同大小的组;1497.检查数组对是否可以被k整除,涉及整除性质的应用。代码演示了如何实现这些算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

七月第七天解题报告

本文实现了超链接跳转,查询题目可直接点击题号跳转LeetCode

目录

970. 强整数
914. 卡牌分组
1497. 检查数组对是否可以被 k 整除
面试题 17.05. 字母与数字

题目

970. 强整数

代码演示
class Solution {
public:
    vector<int> powerfulIntegers(int x, int y, int bound) {
        vector<int>xs, ys;
        int i, j;
        int prex = 1, prey = 1;
        for(i = 1; prex <= bound; ++i){
            xs.push_back(prex);
            prex *= x;
            if(x == 1){
                break;
            }
        }
        for(i = 1; prey <= bound; ++i){
            ys.push_back(prey);
            prey *= y;
            if(y == 1){
                break;
            }
        }
        int hash[1000010];
        memset(hash, 0, sizeof(hash));
        for(i = 0; i < xs.size(); ++i){
            for(j = 0; j < ys.size(); ++j){
                int val = xs[i] + ys[j];
                if(val <= bound){
                    hash[val] = 1;
                }
            }
        }
        vector<int>ans;
        for(i = 1; i <= 1000000; ++i){
            if(hash[i])
                ans.push_back(i);
        }
        return ans;
    }
};

914. 卡牌分组

代码演示
class Solution {
    int gcd(int a,int b){
        return !b ? a : gcd(b, a%b);
    }
public:
    bool hasGroupsSizeX(vector<int>& deck) {
        int hash[10010];
        memset(hash, 0, sizeof(hash));
        for(int i = 0; i < deck.size(); ++i){
            ++hash[ deck[i] ];
        }
        int totgcd = -1;
        for(int i = 0; i < 10000; ++i){
            if(hash[i]){
                if(totgcd == -1){
                totgcd = hash[i];
                }else{
                totgcd = gcd(totgcd, hash[i]);
                }
            }
        }
        return totgcd > 1;
    }
};

1497. 检查数组对是否可以被 k 整除

代码演示

面试题 17.05. 字母与数字

代码演示

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

测试小白~o(〃'▽'〃)o

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值