微信红包
来源:互联网 发布:retrofit2 提交json 编辑:程序博客网 时间:2024/04/29 08:32
春节期间小明使用微信收到很多个红包,非常开心。在查看领取红包记录时发现,某个红包金额出现的次数超过了红包总数的一半。请帮小明找到该红包金额。写出具体算法思路和代码实现,要求算法尽可能高效。
给定一个红包的金额数组gifts及它的大小n,请返回所求红包的金额。
若没有金额超过总数的一半,返回0。测试样例:
[1,2,3,2,2],5
返回:2
#include<iostream>using namespace std;#include<vector>#include<algorithm>class Gift {public:int getValue(vector<int> gifts, const int n) {// write code heresort(gifts.begin(), gifts.end());//先进行排序int count = 0;//记录次数countfor (int i = 0; i < n; i++){//红包金额的数目超过红包的一半,那么这个数字一定在和中位数相等if (gifts[i] == gifts[n/2])//如果和排序后的中位数相等,count++count++;}if (count > (n/2))//n/2代表红包个数的一半,如果count大于数目一半return gifts[n/2];//返回这个金额return 0;//否则返回0}};int main(){Gift gf;vector<int> v({ 1, 2, 3, 2, 2 });int ret=gf.getValue(v, 5);cout << ret << endl;system("pause");return 0;}
1 0
- 微信红包
- 微信红包算法
- 微信现金红包
- 微信红包算法
- 微信红包
- 微信红包
- 微信红包
- 微信裂变红包
- 微信处理红包
- 微信红包算法
- 微信红包算法
- 微信红包算法
- 微信红包
- 模仿微信红包
- 微信红包算法
- 微信红包
- 微信红包
- 微信红包架构
- [完]机器学习实战 第五章 Logistic回归(Logistic Regression)
- 快速排序算法C++实现
- 配置apache的80、443端口
- ubuntu14.04 X64 iNodeClient.sh
- printf的归宿-数据打印到哪儿了
- 微信红包
- 递归习题GCD算法
- angular controller不起作用解决方案
- HTTP和HTTPS区别
- Redis探索之旅(7)- Redis数据类型之列表
- 【C#MVC4】菜单管理和访问权限分配(四)
- POJ2299——归并排序求逆序对数
- C# datable clone与copy的区别
- 第九期 HG255d硬件分析 《路由器就是开发板》