七月day7——哈希表

T1:leetcode.970 强整数

image-20220707150155861

class Solution {
public:
    vector<int> powerfulIntegers(int x, int y, int bound) {
       vector<int> xs;
       vector<int> ys;
       vector<int> result;
       vector<int> ans;
       int prex = 1;
       int prey = 1;
       int hash[1000010];
       int i, j;
       memset(hash, 0, sizeof(hash));

       for (i = 0; prex <= bound; i++) {
           xs.push_back(prex);
           prex *= x;
           if (x == 1) break;
       }
       for (j = 0; prey <= bound; j++) {
           ys.push_back(prey);
           prey *= y;
           if (y == 1) break;
       }
       for (i = 0; i < xs.size(); i++) {
           for (j = 0; j < ys.size(); j++) {
               if(xs[i] + ys[j] <= bound) {
                  hash[xs[i] + ys[j]] = 1;
               }
           }
       }

       for (i = 1; i <= 1000000; ++i) {
           if (hash[i]) {
              result.push_back(i);
           }
       }
       return result;
    }
};
/*
1.push_back()是一个函数
2.先取出两边所有可能小于bound的集合存入xs和ys 不同和形参变量x和y命名相同
3.暴力遍历两层,得到所有结果后用hash数组去重
4.最后再利用hash数组的值,将去重后下标插入
*/

T2:leetcode914. 卡牌分组

image-20220707152818850

class Solution {
public:
    bool hasGroupsSizeX(vector<int>& deck) {
       map<int, int> hash_map;
       for (int i = 0; i < deck.size(); i++) {
           hash_map[deck[i]]++;
       }
       int g = hash_map.begin()->second;
       for (auto i = hash_map.begin(); i != hash_map.end(); i++) {
           g = gcd(g, i->second);
       }
       return g >= 2;
    }
};
/*
1.求最大公约数 用gcd
2.hash_map 键值对 key存储了deck里的不同的元素 value存储了具体元素的个数
*/
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

-特立独行的猪-

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

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

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

打赏作者

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

抵扣说明:

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

余额充值