Published: 03 Mar 2018 › Updated: 03 Mar 2018![[LeetCode] 677. Map Sum Pairs (Python3)](/nhc-explorer-cover.webp)
![[LeetCode] 677. Map Sum Pairs (Python3)](/nhc-explorer-cover.webp)
[LeetCode] 677. Map Sum Pairs (Python3)
https://leetcode.com/problems/map-sum-pairs/description/
Implement a MapSum class with insert, and sum methods.
Check the website above for more details
Medium, 44ms, 100%
class MapSum:
## Initialize the dictionary
def __init__(self):
self.wdict = {}
## Insert keys and value
## Since values will be overrided for the already existing key, just one line below will be enough
def insert(self, key, val):
self.wdict[key] = val
## if the first len(prefix) letters equals to the prefix, add the key to the sum
def sum(self, prefix):
sum = 0
for key in self.wdict:
if key[:len(prefix)] == prefix:
sum += self.wdict[key]
return sum
Without Comments
class MapSum:
def __init__(self):
self.wdict = {}
def insert(self, key, val):
self.wdict[key] = val
def sum(self, prefix):
sum = 0
for key in self.wdict:
if key[:len(prefix)] == prefix:
sum += self.wdict[key]
return sumLeave [LeetCode] 677. Map Sum Pairs (Python3) to:
Read more #kr posts
Best Posts From juneepa
We have not curated any of juneepa's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From juneepa
- 3월 4일자 해외축구 이적 루머: 베예린, 아자르, 조르지뉴, 바란, 발로텔리, 안드레 실바
- [BBC Football] 3월 4일차 해외축구 이적 루머: 이니에스타, 바디, 이브라히모비치, 세세뇽, 램지
- [BBC Football] 3월 2일자 해외축구 이적 루머: 베예린, 벵거, 발로텔리, 펠라이니, 조르지뉴
- [LeetCode] 677. Map Sum Pairs (Python3)
- [BBC Football] 3월 1일자 해외축구 이적 루머: 네이마르, 벵거, 앙리, 에반스
- [BBC Football] 3월 2일자 해외축구 이적 루머: 로이스, 쿠르트와, 조르지뉴, 발로텔리