题解 | #微信红包#

微信红包

https://www.nowcoder.com/practice/fbcf95ed620f42a88be24eb2cd57ec54

hashmap遍历
import java.util.*;

public class Gift {
    public int getValue(int[] gifts, int n) {
        // write code here
        Map<Integer,Integer> countMap=new HashMap<>();
        for(int gift:gifts){
            int count=countMap.getOrDefault(gift,0);
            if(count+1>n/2){
                 return gift;
                 
            }
            countMap.put(gift,countMap.getOrDefault(gift,0)+1);
        }
        return 0;
    }
}


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务