677. Map Sum Pairs
来源:互联网 发布:javlib最新域名2017 编辑:程序博客网 时间:2024/06/03 13:41
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: NullInput: sum("ap"), Output: 3Input: insert("app", 2), Output: NullInput: sum("ap"), Output: 5
实现MapSum 数据结构,其中包含两个方法insert和sum。insert是记录一个字符串-整数对。sum是求所有键值的前缀为prefix的对的数字和。使用unordered_map记录键值对。实现starts_with函数来判断是否是前缀。则insert和sum方法很容易就实现了。
代码:
class MapSum {public: /** Initialize your data structure here. */ MapSum() { } void insert(string key, int val) { pairs[key] = val; } int sum(string prefix) { int res = 0; for(auto p:pairs) { if(starts_with(p.first, prefix)) { res += p.second; } } return res; }private: unordered_map<string, int> pairs; bool starts_with(string s, string t) { return s.substr(0, t.size()) == t; }};
阅读全文
0 0
- 677. Map Sum Pairs
- 677. Map Sum Pairs
- leetcode 677. Map Sum Pairs
- leetcode 677. Map Sum Pairs
- 677. Map Sum Pairs(JAVA)
- leetcode 677. Map Sum Pairs
- LeetCode 677. Map Sum Pairs
- leetcode 677. Map Sum Pairs
- LWC 50:677. Map Sum Pairs
- LeetCode677. Map Sum Pairs
- Map Sum Pairs问题及解法
- 第14周Map Sum Pairs
- 未完成--Sum of Pairs
- Rust : codewars的Sum of Pairs
- pairs
- pairs
- Two Sum 的map函数
- Incorrect inline parameter map format (missmatched name=value pairs)
- 用Skyline二次开发接口创建Message弹框
- bash, sh, dash 傻傻分不清楚
- virtualbox 虚拟机 win7和linux ping不通的解决方法
- java8简单日期时间工具类
- 【jvm实战】IDEA运行速度调优
- 677. Map Sum Pairs
- 游标的定义、显示游标、隐式游标语法
- python---urllib2使用代理
- vue中获取v-for中的dom元素
- spring boot(四):thymeleaf使用详解
- switch语句能否作用在 byte 上,能否作用在 long 上,能否作用在 String 上?
- 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用
- 再见OI,NOIP2017退役
- zookeeper解决Error contacting service. It is probably not running.