题解 | 大整数哈希

大整数哈希

https://www.nowcoder.com/practice/29f0cff8a69b4ab6a2f63fb7386defa3

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        // 消耗空行
        in.nextLine();
        // 大小特别大不能创造这么大的数组,我们就维护一个hash表,若这个数存在就代表已经改过数据
        // 若没有改过就代表为0
        long res = 0L;
        Map<Long,Long> maps = new HashMap<>();
        for(int i = 1 ; i <= n ; i++){
            String[] parts=in.nextLine().split(" ");
            long x = Long.parseUnsignedLong(parts[0]);
            long y = Long.parseUnsignedLong(parts[1]);
            long ans=maps.getOrDefault(x,0L);
            maps.put(x,y);
            res = res + i*ans;
        }
        System.out.println(Long.toUnsignedString(res));
    }
}

全部评论

相关推荐

11-07 15:54
门头沟学院 Java
点赞 评论 收藏
分享
10-21 16:54
门头沟学院 Java
后端转测开第一人:微服务没用 校招都不看微服务的 还有就是后端行情是这样的 找实习纯看运气 秋招更是吃运气和缘分 如果对代码没有极致的追求 可以转测开
应届生简历当中,HR最关...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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