Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 69a8cb0

Browse files
committed
677 solved
1 parent 645b75b commit 69a8cb0

File tree

5 files changed

+88
-4
lines changed

5 files changed

+88
-4
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ continually updating 😃.
119119
* [307. Range Sum Query - Mutable](src/0307_Range_Sum_Query_Mutable/range_sum_query_mut.go)   *`segment tree`*
120120
* [404. Sum of Left Leaves](src/0404_sum_of_left_leaves/sum_of_left_leaves.go)   *`binary tree`*
121121
* [437. Path Sum III](src/0437_path_sum_3/path_sum_3.go)   *`binary tree`*
122+
* [677. Map Sum Pairs](src/0677_map_sum_pairs/map_sum_pairs.go)   *`trie`*
122123
* [872. Leaf-Similar Trees](src/0872_leaf_similar_trees/leaf_similar_trees.go)   *`binary tree`*
123124

124125
### Binary Search

‎src/0208_implement_trie_prefix_tree/impltrie.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (trie *Trie) Insert(word string) {
3636
if _, ok := cur.next[word[i]]; !ok {
3737
cur.next[word[i]] = &node{next: make(map[byte]*node)}
3838
}
39-
cur, _ = cur.next[word[i]]
39+
cur = cur.next[word[i]]
4040
}
4141
if !cur.isWord {
4242
cur.isWord = true
@@ -52,9 +52,8 @@ func (trie *Trie) Search(word string) bool {
5252
if _, ok := cur.next[word[i]]; !ok {
5353
return false
5454
}
55-
cur, _ = cur.next[word[i]]
55+
cur = cur.next[word[i]]
5656
}
57-
5857
return cur.isWord
5958
}
6059

@@ -66,7 +65,7 @@ func (trie *Trie) StartsWith(prefix string) bool {
6665
if _, ok := cur.next[prefix[i]]; !ok {
6766
return false
6867
}
69-
cur, _ = cur.next[prefix[i]]
68+
cur = cur.next[prefix[i]]
7069
}
7170
return true
7271
}

‎src/0677_map_sum_pairs/map_sum_pairs.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
677. Map Sum Pairs
3+
https://leetcode.com/problems/map-sum-pairs/
4+
5+
Implement a MapSum class with insert, and sum methods.
6+
7+
For the method insert, you'll be given a pair of (string, integer).
8+
The string represents the key and the integer represents the value.
9+
If the key already existed, then the original key-value pair will be overridden to the new one.
10+
11+
For the method sum, you'll be given a string representing the prefix,
12+
and you need to return the sum of all the pairs' value whose key starts with the prefix.
13+
*/
14+
// time: 2019年02月01日
15+
16+
package mapsumpairs
17+
18+
type node struct {
19+
val int
20+
next map[rune]*node
21+
}
22+
23+
// MapSum data structure for solution.
24+
type MapSum struct {
25+
root *node
26+
}
27+
28+
// Constructor initialize data structure here.
29+
func Constructor() MapSum {
30+
return MapSum{&node{next: make(map[rune]*node)}}
31+
}
32+
33+
// Insert inserts a word into the trie.
34+
func (ms *MapSum) Insert(key string, val int) {
35+
cur := ms.root
36+
for _, c := range key {
37+
if _, ok := cur.next[c]; !ok {
38+
cur.next[c] = &node{next: make(map[rune]*node)}
39+
}
40+
cur = cur.next[c]
41+
}
42+
cur.val = val
43+
}
44+
45+
// Sum sum of all the pairs' value whose key starts with the prefix.
46+
func (ms *MapSum) Sum(prefix string) int {
47+
cur := ms.root
48+
for _, c := range prefix {
49+
if _, ok := cur.next[c]; !ok {
50+
return 0
51+
}
52+
cur = cur.next[c]
53+
}
54+
return sum(cur)
55+
}
56+
57+
func sum(n *node) int {
58+
res := n.val
59+
for _, nextNode := range n.next {
60+
res += sum(nextNode)
61+
}
62+
return res
63+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package mapsumpairs
2+
3+
import "testing"
4+
5+
func TestMapSumPairs(t *testing.T) {
6+
obj := Constructor()
7+
obj.Insert("apple", 3)
8+
if res := obj.Sum("ap"); res != 3 {
9+
t.Errorf("expected %d, got %d", 3, res)
10+
}
11+
12+
obj.Insert("app", 2)
13+
if res := obj.Sum("ap"); res != 5 {
14+
t.Errorf("expected %d, got %d", 5, res)
15+
}
16+
17+
if res := obj.Sum("al"); res != 0 {
18+
t.Errorf("expected %d, got %d", 0, res)
19+
}
20+
}

‎src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
|0455|[Assign Cookies](./0455_assign_cookies/assign_cookies.go)|Easy|*`greedy algorithm`*|
103103
|0557|[557. Reverse Words in a String III](0557_reverse_words_in_a_string_3/reverse_words_in_a_string_3.go)|Easy|*`string`*|
104104
|0674|[674. Longest Continuous Increasing Subsequence](0674_longest_continuous_increasing_subsequence/lcis.go)|Easy||
105+
|0677|[677. Map Sum Pairs](0677_map_sum_pairs/map_sum_pairs.go)|Medium|*`trie`*|
105106
|0704|[Binary Search](0704_binary_search/binary_search.go)|Easy|*`binary search`*|
106107
|0713|[713. Subarray Product Less Than K](0713_subarray_product_less_than_k/spltk.go)|Medium|*`sliding window`*|
107108
|0717|[717. 1-bit and 2-bit Characters](0717_1_bit_and_2_bit_characters/1bitand2bitc.go)|Easy||

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /