春节期间小明使用微信收到很多个红包,非常开心。在查看领取红包记录时发现,某个红包金额出现的次数超过了红包总数的一半。请帮小明找到该红包金额。写出具体算法思路和代码实现,要求算法尽可能高效。
给定一个红包的金额数组 gifts 及它的大小 n ,请返回所求红包的金额。
若没有金额超过总数的一半,返回0。 数据范围:
,红包金额满足 
春节期间小明使用微信收到很多个红包,非常开心。在查看领取红包记录时发现,某个红包金额出现的次数超过了红包总数的一半。请帮小明找到该红包金额。写出具体算法思路和代码实现,要求算法尽可能高效。
给定一个红包的金额数组 gifts 及它的大小 n ,请返回所求红包的金额。
若没有金额超过总数的一半,返回0。[1,2,3,2,2],5
2
[1,1,2,2,3,3],6
0
using System.Collections.Generic;
class Gift
{
public int getValue(int[] gifts, intn)
{
Dictionary<int, int> Gifts = new Dictionary<int,int>();
for(int Index = 0; Index < n; Index++)
{
if(!Gifts.ContainsKey(gifts[Index]))
Gifts.Add(gifts[Index], 1);
else
Gifts[gifts[Index]]++;
}
foreach(KeyValuePair<int, int> Pair in Gifts)
{
if(Pair.Value > n / 2)
return Pair.Key;
}
return 0;
}
}
class Gift {
public:
int getValue(vector<int> gifts, int n) {
// write code here
int rslt = gifts[0];
int cnt = 1;
int nums = 0;
if(1 == n)
return rslt;
for(int i = 1;i < n;++i) {
if(0 == cnt) {
rslt = gifts[i];
cnt = 1;
continue;
}
if(gifts[i] == rslt)
++cnt;
else {
--cnt;
}
}
for(int i = 0;i < n;++i) {
if(rslt == gifts[i])
++nums;
}
if(nums > n/2)
return rslt;
else
return -1;
}
};
众数问题,摩尔投票法
class Gift {
public:
int getValue(vector<int> gifts, int n) {
int cnt=0;
int tmp;
for(int i=0;i<n;i++){
if(cnt==0||gifts[i]==tmp){
if(cnt==0)
tmp=gifts[i];
cnt++;
}
else cnt--;
}
cnt=0;
for(int i=0;i<n;i++){
if(gifts[i]==tmp)
cnt++;
}
if(cnt>n/2)
return tmp;
else
return 0;
}
};
class Gift {
public:
int getValue(vector<int> gifts, int n) {
int i = 0;
int j = 1;
int temp = gifts[0];//初始化,temp先保存第一个数字
int count = 1;//count初始化1,表示有1个相同的数字
while (j<n)
{
if (count == 0)
{
temp = gifts[j];
count++;
j++;
}
if (j >= n)
break;
if (temp == gifts[j])
{
count++;
j++;
}
else
{
count--;
j++;
}
}
if (count == 0) //count==0,不存在
{
return 0;
}
//但是count!=0,并不能说明不存在,因为我们要判断temp是不是有一半以上。
count = 0;
for (int i = 0; i < n; i++)
{
if (gifts[i] == temp)
count++;
}
return (count > n / 2 ? temp : 0);
}
};
int getValue(vector<int> gifts, int n){map<int,int> r;for(int i = 0; i < n; i++)if(++r[gifts[i]]>n/2)return gifts[i];return 0;}
import java.util.*;
public class Gift {
public int getValue(int[] gifts, int n) {
// write code here
int money=0;
TreeMap<Integer,Integer> map=new TreeMap<Integer,Integer>();
for(int i=0;i<n;i++){
if(n>1){
if(map.get(gifts[i])==null)
map.put(gifts[i],1);
else{
int number=map.get(gifts[i]).intValue();
number++;
if(number>n/2)
return money=gifts[i];
map.put(gifts[i],number);
}
}
}
return money;
}
}
#include <algorithm>
class Gift {
public:
int getValue(vector<int> gifts, int n) {
// write code here
//先排序一下,然后判断一下中间的数与1/4处数或3/4处数是否相同,若相同则返回此数
//若不相同,则返回0
sort(gifts.begin(),gifts.end());
int half = n/2;
int start = n/4;
int last = start*3;
if((gifts[start]==gifts[half])||(gifts[half]==gifts[last])){
return gifts[half];
}else{
return 0;
}
}
}; class Gift {
public:
int getValue(vector<int> gifts, int n) {
// write code here
int mid = (n+1)/2;
unordered_map<int,int> map;
for(int i = 0;i<n;i++){
map[gifts[i]]++;
}
for(auto it=map.begin();it!= map.end();it++){
if(it->second >= mid) return it->first;
}
return 0;
}
}; int getValue(vector<int> gifts, int n) {
// write code here
int len=gifts.size()/2;
for(int i=0;i<gifts.size();++i)
{
if(count(gifts.begin(),gifts.end(),gifts[i])>len)
{
return gifts[i];
}
}
return 0;
}
stl count()大法好
class Gift {
public:
int getValue(vector<int> gifts, int n) {
// write code here
sort(gifts.begin(), gifts.end());
if (gifts[n/4] == gifts[n/2] || gifts[3*n/4] == gifts[n/2])
return gifts[n/2];
return 0;
}
}; 既然超过了一半 ,那么排序后,中位数肯定是最多出现的不用说,而且肯定会覆盖【1/4,2/4】,【2/4,3/4】这两个区间中的一个,举例:7个数字【1,2,3,3,3,3,5】class Gift {
public:
int getValue(vector<int> gifts, int n) {
// write code here
int ret = gifts[0];
int count = 1;
for(size_t i = 1; i<n; ++i)
{
if(count==0)
{
ret = gifts[i];
count = 1;
}
else if(ret == gifts[i])
count++;
else
count--;
}
return count==0 ? 0 : ret;
}
}; class Gift {
public:
int getValue(vector<int> gifts, int n) {
sort(gifts.begin(),gifts.end());
int ret = gifts[(n/2) - 1];
int count = 0;
for(int i = 0;i < n;i++)
{
if(gifts[i] == ret)
{
count++;
}
}
if(count>(n/2))
{
return ret;
}
else
{
return 0;
}
}
};
之前做题就做到过类似的,出现最多的大于总数的二分之一,那么排序之后,一定在中间数或者前一位
import java.util.*;
public class Gift {
public int getValue(int[] gifts, int n) {
// write code here
HashMap<Integer,Integer> hashMap=new HashMap<Integer,Integer>();
int half=n/2;
for(int i=0;i<n;i++){
if(hashMap.get(gifts[i])==null){
hashMap.put(gifts[i],1);
}else{
int count=hashMap.get(gifts[i])+1;
if(count>half)
return gifts[i];
hashMap.put(gifts[i],count);
}
}
return 0;
}
} //使用map记录出现的次数 public static int getValue(int[] gifts, int n) { Map<Integer, Integer> map = new HashMap(); Integer re = gifts[0]; int max = 0; for (int i = 0; i < gifts.length; i++) { if (map.containsKey(gifts[i])) { map.put(gifts[i], map.get(gifts[i]) + 1); } else { map.put(gifts[i], 1); } if (map.get(gifts[i]) > max) { max = map.get(gifts[i]); re = gifts[i]; } } if (max > gifts.length / 2) { return re; } else { return 0; } }
import java.util.*;
public class Gift {
public int getValue(int[] gifts, int n) {
// write code here
TreeMap<Integer,Integer> map = new TreeMap<Integer,Integer>();
for(int i = 0;i < gifts.length;i++){
Integer key = new Integer(gifts[i]);
if(map.get(key) == null){
map.put(key,1);
}
else{
int value = map.get(key).intValue();
value++;
map.put(key,value);
if(value >(double)n*1.0/2)return key.intValue();
}
}
return 0;
}
}
采用了树型图,还看了书.