File tree Expand file tree Collapse file tree 5 files changed +88
-4
lines changed
0208_implement_trie_prefix_tree Expand file tree Collapse file tree 5 files changed +88
-4
lines changed Original file line number Diff line number Diff line change @@ -119,6 +119,7 @@ continually updating 😃.
119
119
* [ 307. Range Sum Query - Mutable] ( src/0307_Range_Sum_Query_Mutable/range_sum_query_mut.go )   ;  ;  ; * ` segment tree ` *
120
120
* [ 404. Sum of Left Leaves] ( src/0404_sum_of_left_leaves/sum_of_left_leaves.go )   ;  ;  ; * ` binary tree ` *
121
121
* [ 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 ` *
122
123
* [ 872. Leaf-Similar Trees] ( src/0872_leaf_similar_trees/leaf_similar_trees.go )   ;  ;  ; * ` binary tree ` *
123
124
124
125
### Binary Search
Original file line number Diff line number Diff line change @@ -36,7 +36,7 @@ func (trie *Trie) Insert(word string) {
36
36
if _ , ok := cur.next [word [i ]]; ! ok {
37
37
cur.next [word [i ]] = & node {next : make (map [byte ]* node )}
38
38
}
39
- cur , _ = cur.next [word [i ]]
39
+ cur = cur.next [word [i ]]
40
40
}
41
41
if ! cur .isWord {
42
42
cur .isWord = true
@@ -52,9 +52,8 @@ func (trie *Trie) Search(word string) bool {
52
52
if _ , ok := cur.next [word [i ]]; ! ok {
53
53
return false
54
54
}
55
- cur , _ = cur.next [word [i ]]
55
+ cur = cur.next [word [i ]]
56
56
}
57
-
58
57
return cur .isWord
59
58
}
60
59
@@ -66,7 +65,7 @@ func (trie *Trie) StartsWith(prefix string) bool {
66
65
if _ , ok := cur.next [prefix [i ]]; ! ok {
67
66
return false
68
67
}
69
- cur , _ = cur.next [prefix [i ]]
68
+ cur = cur.next [prefix [i ]]
70
69
}
71
70
return true
72
71
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 102
102
| 0455| [ Assign Cookies] ( ./0455_assign_cookies/assign_cookies.go ) | Easy| * ` greedy algorithm ` * |
103
103
| 0557| [ 557. Reverse Words in a String III] ( 0557_reverse_words_in_a_string_3/reverse_words_in_a_string_3.go ) | Easy| * ` string ` * |
104
104
| 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 ` * |
105
106
| 0704| [ Binary Search] ( 0704_binary_search/binary_search.go ) | Easy| * ` binary search ` * |
106
107
| 0713| [ 713. Subarray Product Less Than K] ( 0713_subarray_product_less_than_k/spltk.go ) | Medium| * ` sliding window ` * |
107
108
| 0717| [ 717. 1-bit and 2-bit Characters] ( 0717_1_bit_and_2_bit_characters/1bitand2bitc.go ) | Easy||
You can’t perform that action at this time.
0 commit comments