力扣-677题 键值映射(C++)- unordered_map

题目链接:https://leetcode-cn.com/problems/map-sum-pairs/
题目如下:
在这里插入图片描述
在这里插入图片描述

class MapSum {
    
    
public:
    unordered_map<string,int> umap;
    MapSum() {
    
    
    }
    
    void insert(string key, int val) {
    
    
        umap[key]=val;
    }
    
    int sum(string prefix) {
    
    
        int sum=0;
        int length1=prefix.size();
        int first_len=0;
        for(auto e:umap){
    
    
            cout<<e.first<<endl;
            first_len=e.first.size();
            if(first_len<length1) continue;
            else if(e.first.substr(0,length1)==prefix) sum+=e.second;
        }

        return 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);
 */

おすすめ

転載: blog.csdn.net/qq_40467670/article/details/121343308