leetcode 677. Map Sum Pairs
来源:互联网 发布:气相色谱图数据怎么看 编辑:程序博客网 时间:2024/06/11 14:58
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
本题和上一题一样直接暴力查询即可,本题的本意应该是使用前缀树,但是这个有点麻烦,所以我这里直接暴力去做
代码如下:
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <functional>#include <bitset>#include <numeric>#include <cmath>#include <regex>using namespace std;class MapSum {public: map<string, int> mmp; /** Initialize your data structure here. */ MapSum() { } void insert(string key, int val) { mmp[key] = val; } int sum(string prefix) { int s = 0; for (auto one : mmp) { if (prefix == one.first.substr(0, prefix.length())) s += one.second; } return s; }};/*** Your MapSum object will be instantiated and called as such:* MapSum obj = new MapSum();* obj.insert(key,val);* int param_2 = obj.sum(prefix);*/
阅读全文
0 0
- leetcode 677. Map Sum Pairs
- leetcode 677. Map Sum Pairs
- leetcode 677. Map Sum Pairs
- LeetCode 677. Map Sum Pairs
- leetcode 677. Map Sum Pairs
- 677. Map Sum Pairs
- 677. Map Sum Pairs
- 677. Map Sum Pairs(JAVA)
- LWC 50:677. Map Sum Pairs
- LeetCode677. Map Sum Pairs
- Map Sum Pairs问题及解法
- 第14周Map Sum Pairs
- LeetCOde OJ Two Sum map应用
- LeetCode 1. Two Sum (c++ stl map)
- 未完成--Sum of Pairs
- [LeetCode]Palindrome Pairs
- LeetCode: Palindrome Pairs
- LeetCode 336. Palindrome Pairs
- java Swing最小化到任务栏图标,单击显示
- 清单文件引用
- 【loj6159】「美团 CodeM 初赛 Round A」最长树链
- 树莓派 liunx 无线路由器
- FineReport 9.0 破解注册原理以及教程
- leetcode 677. Map Sum Pairs
- Elasticsearch关于unassigned shards修复
- Python3 初学实践案例(11)判断质数以及计算一个数字的质因数
- hdu4292 food
- 集成开发环境VS 程序调试时逐语句与逐过程的区别
- 64位win10系统无法安装.Net framework3.5的两种解决方法
- 微服务治理之道
- Java之学习过错(三)
- Python笔记 NO.2