zoukankan      html  css  js  c++  java
  • 腾讯在线模拟试题 微信红包

    春节期间小明使用微信收到很多个红包,非常开心。在查看领取红包记录时发现,某个红包金额出现的次数超过了红包总数的一半。请帮小明找到该红包金额。写出具体算法思路和代码实现,要求算法尽可能高效。

    给定一个红包的金额数组gifts及它的大小n,请返回所求红包的金额。

    测试样例:
    [1,2,3,2,2],5
    返回:2
    建立一个hash表即可
    class Gift {
    public:
        int getValue(vector<int> gifts, int n) {
            // write code here
            int max=0,cnt=0;
            map<int,int> iIntMap;
            for(auto &e:gifts)
                {
                iIntMap[e]+=1;
            }
            for(auto &e:iIntMap)
                {
                if(e.second>cnt)
                {
                    max=e.first;
                    cnt=e.second;
                }
            }
            if(cnt*2>n)
            return max;
            return 0;
        }
    };
    添加笔记
  • 相关阅读:
    MongoDB Shell
    mongo 日记
    java 堆栈 静态
    面向对象(2)
    面向对象(1)
    mongo 学习笔记
    深入浅出学Spring Data JPA
    java记录
    mongodb 2.6 window 安装启动服务
    CF1012F Passports
  • 原文地址:https://www.cnblogs.com/csudanli/p/5344597.html
Copyright © 2011-2022 走看看