LeetCode - 677. Map Sum Pairs(键值映射)(字典树变形)

33 篇文章 0 订阅

LeetCode - 677. Map Sum Pairs(键值映射)(字典树变形)

题目链接
题目

在这里插入图片描述

解析

做这题之前先学字典树基础。这个题目和普通字典树不同的是结点内部存放的是val,一个具体的值,不是pathend

  • insert()也没什么好说的,注意找到末尾结点之后,维护val
  • 注意求这里的sum,分为两部,第一步先找到对应字符串的结尾结点,然后使用递归来求解它所有孩子的结点的值的和;

这里写图片描述

class MapSum {
    
    private class Node{
        public int val;
        public Node[] next;//使用整数表示字符 c - 'a'

        public Node() {
            val = 0;
            next = new Node[26];
        }
    }

    private Node root;

    public MapSum() {
        root = new Node();
    }

    public void insert(String key, int val) {
        if(key == null)
            return;
        Node cur = root;
        int index = 0;
        for(int i = 0; i < key.length(); i++){
            index = key.charAt(i) - 'a';
            if(cur.next[index] == null)
                cur.next[index] = new Node();
            cur = cur.next[index];
        }
        cur.val = val;
    }

    public int sum(String prefix) {
        if(prefix == null)
            return 0;
        Node cur = root;
        int index = 0;
        for(int i = 0; i < prefix.length(); i++){
            index = prefix.charAt(i) - 'a';
            if(cur.next[index] == null)
                return 0;
            cur = cur.next[index];
        }

        //开始递归求解 所有孩子的值的和
        return process(cur);
    }

    public int process(Node node){
        if(node == null)
            return 0;
        int sum = node.val;
        for(Node cur : node.next){
            sum += process(cur);
        }
        return sum;
    }
}

使用HashMap来替代数组next的写法:

class MapSum {

    private class Node{
        public int val;
        public HashMap<Character, Node> nexts;

        public Node() {
            val = 0;
            nexts = new HashMap<>();
        }
    }

    private Node root;

    public MapSum() {
        root = new Node();
    }

    public void insert(String key, int val) {
        if(key == null)
            return;
        Node cur = root;
        for(int i = 0; i < key.length(); i++){
            char c = key.charAt(i);
//            Node nxt = cur.nexts.get(c);
//            if(nxt == null) {
//                nxt = new Node();
//                cur.nexts.put(c, nxt);
//            }
            cur.nexts.computeIfAbsent(c, k -> new Node());
            cur = cur.nexts.get(c);
        }
        cur.val = val;
    }

    public int sum(String prefix) {
        if(prefix == null)
            return 0;
        Node cur = root;
        int index = 0;
        for(int i = 0; i < prefix.length(); i++){
            char c = prefix.charAt(i);
            if(cur.nexts.get(c) == null)
                return 0;
            cur = cur.nexts.get(c);
        }
        //开始递归求解 所有孩子的值的和
        return process(cur);
    }

    public int process(Node node){
        if(node == null)
            return 0;
        int sum = node.val;
        for(Map.Entry<Character, Node>entry : node.nexts.entrySet()){
            sum += process(entry.getValue());
        }
        return sum;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值