Skip to content

Commit cf3d49c

Browse files
add 677
1 parent f8f045f commit cf3d49c

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Your ideas/fixes/algorithms are more than welcome!
2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
2525
|680|[Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_680.java) | O(n) | O(1) | Easy | String
26+
|677|[Map Sum Pairs](https://leetcode.com/problems/map-sum-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_677.java) | O(n) | O(n) | Medium | HashMap
2627
|676|[Implement Magic Dictionary](https://leetcode.com/problems/implement-magic-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_676.java) | O(n^2) | O(n) | Medium |
2728
|674|[Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_674.java) | O(n^2) | O(1) | Easy |
2829
|672|[Bulb Switcher II](https://leetcode.com/problems/bulb-switcher-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_672.java) | O(1) | O(1) | Medium | Math
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.fishercoder;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 677. Map Sum Pairs
8+
*
9+
* Implement a MapSum class with insert, and sum methods.
10+
11+
For the method insert, you'll be given a pair of (string, integer).
12+
The string represents the key and the integer represents the value.
13+
If the key already existed, then the original key-value pair will be overridden to the new one.
14+
15+
For the method sum, you'll be given a string representing the prefix,
16+
and you need to return the sum of all the pairs' value whose key starts with the prefix.
17+
18+
Example 1:
19+
20+
Input: insert("apple", 3), Output: Null
21+
Input: sum("ap"), Output: 3
22+
Input: insert("app", 2), Output: Null
23+
Input: sum("ap"), Output: 5
24+
25+
*/
26+
public class _677 {
27+
public static class Solution1 {
28+
public static class MapSum {
29+
30+
Map<String, Integer> map;
31+
32+
/**
33+
* Initialize your data structure here.
34+
*/
35+
public MapSum() {
36+
map = new HashMap<>();
37+
}
38+
39+
public void insert(String key, int val) {
40+
map.put(key, val);
41+
}
42+
43+
public int sum(String prefix) {
44+
int sum = 0;
45+
for (String key : map.keySet()) {
46+
if (key.startsWith(prefix)) {
47+
sum += map.get(key);
48+
}
49+
}
50+
return sum;
51+
}
52+
}
53+
54+
}
55+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy