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

发布于 2024-07-15 08:30:42 字数 3492 浏览 20 评论 0

题目

解析

这个题目和普通字典树不同的是结点内部存放的是 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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

昨迟人

暂无简介

文章
评论
25 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文