zoukankan      html  css  js  c++  java
  • leetcode 677. Map Sum Pairs

    Implement a MapSum class with insert, and sum methods.

    For the method insert, you'll be given a pair of (string, integer). The string represents the key and the integer represents the value. If the key already existed, then the original key-value pair will be overridden to the new one.

    For the method sum, you'll be given a string representing the prefix, and you need to return the sum of all the pairs' value whose key starts with the prefix.

    Example 1:
    Input: insert("apple", 3), Output: Null
    Input: sum("ap"), Output: 3
    Input: insert("app", 2), Output: Null
    Input: sum("ap"), Output: 5
    

    题目大意:
    实现一个Mapsum,Maxsum包括两个操作:
    insert:插入字符串和相应的值
    sum("xxx"):查询以xxx为前缀的字符串的值的和
    思路:
    一看就是字典树啊,查询的时候套个dfs就行了

    
    class MapSum {
    public:
        class node {
          public:
            node* next[26];
            int end;
            node() {
                for (int i = 0; i < 26; ++i) {
                    next[i] = nullptr;
                }
                end = 0;
            }
        };
        node* root;
        /** Initialize your data structure here. */
        MapSum() {
            root = new node();
        }
        
        void insert(string key, int val) {
            if (key.size() == 0) return ;
            node* p = root;
            for (int i = 0; i < key.size(); ++i) {
                int x = key[i] - 'a';
                if (p->next[x] == nullptr) {
                    p->next[x] = new node();
                    //p->end += val;
                    p = p->next[x];
                } else {
                    //p->end += val;
                    p = p->next[x];
                }
            }
            p->end = val; 
        }
        
        int sum(string prefix) {
            node* p = root;
            int sum = 0;
            for (int i = 0; i < prefix.size(); ++i) {
                int x = prefix[i] - 'a';
                if (p->next[x] != nullptr) {
                    p = p->next[x];
                } else {
                    return false;
                }
            }
            //sum += p->end;
            dfs(p, sum);
            return sum;
        }
        void dfs(node *p, int& sum) {
            sum += p->end;
            for (int i = 0; i < 26; ++i) {
                if (p->next[i] != nullptr) {
                    dfs(p->next[i], sum);
                }
            }
        }
    };
    
    /**
     * Your MapSum object will be instantiated and called as such:
     * MapSum obj = new MapSum();
     * obj.insert(key,val);
     * int param_2 = obj.sum(prefix);
     */
    
  • 相关阅读:
    基于node.js+socket.io+html5实现的斗地主游戏(1)概述
    [javascript]switchTab:仿腾讯首页Tab栏切换js插件
    [js]利用闭包向post回调函数传参数
    [CSS]利用伪元素实现一些特殊图形 from baidu校招
    [javascript]模块化&命名污染—from 编程精解
    [随手记]2014前端面试题解
    [IE bug] ajax请求 304解决方案
    [java]基于UDP的Socket通信Demo
    [JQuery]ScrollMe滚动特效插件
    java.lang.OutOfMemoryError及解决方案
  • 原文地址:https://www.cnblogs.com/pk28/p/7583634.html
Copyright © 2011-2022 走看看