栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

力扣每日一题2021-11-14键值映射

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

力扣每日一题2021-11-14键值映射

键值映射
  • 677.键值映射
    • 题目描述
    • 思路:扫描、前缀树
      • 扫描
        • Python实现
        • Java实现
      • 前缀树
        • Python实现
        • Java实现


677.键值映射 题目描述

键值映射


思路:扫描、前缀树 扫描

将键值对进行存储,搜索给定的前缀prefix时,依次搜索所有键值,如果键值包含给定的前缀,则将其val进行相加,返回所有符合要求的val的和。

Python实现

# 暴力扫描
class MapSum:

    def __init__(self):
        self.map = {}


    def insert(self, key: str, val: int) -> None:
        self.map[key] = val


    def sum(self, prefix: str) -> int:
        res = 0
        for key, val in self.map.items():
            if key.startswith(prefix):
                res += val
        return res
Java实现

class MapSum {
    Map map;

    public MapSum() {
        map = new HashMap<>();
    }
    
    public void insert(String key, int val) {
        map.put(key,val);
    }
    
    public int sum(String prefix) {
        int res = 0;
        for (String s: map.keySet()) {
            if (s.startsWith(prefix)) {
                res += map.get(s);
            }
        }
        return res;
    }
}
前缀树

由于要处理前缀,很自然就想到了前缀树。具体来说,就是在前缀树的每个节点存储该前缀对应的值。

Python实现

# 前缀树
class TrieNode:
    def __init__(self):
        self.val = 0
        self.next = [None for _ in range(26)]


class MapSum:
    def __init__(self):
        self.root = TrieNode()
        self.map = {}


    def insert(self, key: str, val: int) -> None:
        value = val
        if key in self.map:
            value -= self.map[key]
        self.map[key] = val
        node = self.root
        for c in key:
            if node.next[ord(c) - ord('a')] is None:
                node.next[ord(c) - ord('a')] = TrieNode()
            node = node.next[ord(c) - ord('a')]
            node.val += value


    def sum(self, prefix: str) -> int:
        node = self.root
        for c in prefix:
            if node.next[ord(c) - ord('a')] is None:
                return 0
            node = node.next[ord(c) - ord('a')]
        return node.val
Java实现

class MapSum {
    class TrieNode {
        int val = 0;
        TrieNode[] next = new TrieNode[26];
    }
    
    TrieNode root;
    Map map;

    public MapSum() {
        root = new TrieNode();
        map = new HashMap<>();
    }
    
    public void insert(String key, int val) {
        int value = val - map.getOrDefault(key, 0);
        map.put(key, val);
        TrieNode node = root;
        for (char c: key.toCharArray()) {
            if (node.next[c - 'a'] == null) {
                node.next[c - 'a'] = new TrieNode();
            }
            node = node.next[c - 'a'];
            node.val += value;
        }
    }
    
    public int sum(String prefix) {
        TrieNode node = root;
        for (char c: prefix.toCharArray()) {
            if (node.next[c - 'a'] == null) {
                return 0;
            }
            node = node.next[c - 'a'];
        }
        return node.val;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/498809.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号