File tree 5 files changed +185
-0
lines changed
5 files changed +185
-0
lines changed Original file line number Diff line number Diff line change @@ -240,6 +240,7 @@ All solutions will be accepted!
240
240
| 114| [ Flatten Binary Tree To Linked List] ( https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/description/ ) | [ java/py/js] ( ./algorithms/FlattenBinaryTreeToLinkedList ) | Medium|
241
241
| 284| [ Peeking Iterator] ( https://leetcode-cn.com/problems/peeking-iterator/description/ ) | [ java/py] ( ./algorithms/PeekingIterator ) | Medium|
242
242
| 328| [ Odd Even Linked List] ( https://leetcode-cn.com/problems/odd-even-linked-list/description/ ) | [ java/py/js] ( ./algorithms/OddEvenLinkedList ) | Medium|
243
+ | 677| [ Map Sum Pairs] ( https://leetcode-cn.com/problems/map-sum-pairs/description/ ) | [ java/py/js] ( ./algorithms/MapSumPairs ) | Medium|
243
244
244
245
# Database
245
246
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Map Sum Pairs
2
+ This problem is easy to solve by Trie
Original file line number Diff line number Diff line change
1
+ class MapSum {
2
+
3
+ private class TrieNode {
4
+ public Map <Character , TrieNode > children ;
5
+ public Integer val ;
6
+
7
+ public TrieNode () {
8
+ children = new HashMap <Character , TrieNode >();
9
+ val = null ;
10
+ }
11
+ }
12
+
13
+ private TrieNode root ;
14
+ /** Initialize your data structure here. */
15
+ public MapSum () {
16
+ root = new TrieNode ();
17
+ }
18
+
19
+ public void insert (String key , int val ) {
20
+ TrieNode node = root ;
21
+ for (char c : key .toCharArray ()) {
22
+ if (node .children .get (c ) == null ) {
23
+ node .children .put (c , new TrieNode ());
24
+ }
25
+ node = node .children .get (c );
26
+ }
27
+ node .val = val ;
28
+ }
29
+
30
+ public int sum (String prefix ) {
31
+ int sum = 0 ;
32
+ TrieNode node = root ;
33
+
34
+ for (char c : prefix .toCharArray ()) {
35
+ if (node .children .get (c ) == null ) {
36
+ return 0 ;
37
+ }
38
+ node = node .children .get (c );
39
+ }
40
+
41
+ sum += node .val == null ? 0 : node .val ;
42
+ List <TrieNode > stack = new ArrayList <TrieNode >(node .children .values ());
43
+
44
+ while (stack .size () > 0 ) {
45
+ node = stack .remove (0 );
46
+ stack .addAll (node .children .values ());
47
+ sum += node .val == null ? 0 : node .val ;
48
+ }
49
+
50
+ return sum ;
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Your MapSum object will be instantiated and called as such:
56
+ * MapSum obj = new MapSum();
57
+ * obj.insert(key,val);
58
+ * int param_2 = obj.sum(prefix);
59
+ */
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Initialize your data structure here.
3
+ */
4
+ var MapSum = function ( ) {
5
+ this . root = new TrieNode ( )
6
+ } ;
7
+
8
+ var TrieNode = function ( ) {
9
+ this . children = { }
10
+ this . val = null
11
+ }
12
+
13
+ /**
14
+ * @param {string } key
15
+ * @param {number } val
16
+ * @return {void }
17
+ */
18
+ MapSum . prototype . insert = function ( key , val ) {
19
+ let node = this . root
20
+
21
+ for ( let i = 0 ; i < key . length ; i ++ ) {
22
+ let char = key [ i ]
23
+ if ( node . children [ char ] === undefined ) {
24
+ node . children [ char ] = new TrieNode ( )
25
+ }
26
+ node = node . children [ char ]
27
+ }
28
+
29
+ node . val = val
30
+ } ;
31
+
32
+ /**
33
+ * @param {string } prefix
34
+ * @return {number }
35
+ */
36
+ MapSum . prototype . sum = function ( prefix ) {
37
+ let sum = 0 ,
38
+ node = this . root
39
+
40
+ for ( let i = 0 ; i < prefix . length ; i ++ ) {
41
+ let char = prefix [ i ]
42
+ if ( node . children [ char ] === undefined ) {
43
+ return 0
44
+ }
45
+ node = node . children [ char ]
46
+ }
47
+
48
+ sum += node . val ? node . val : 0
49
+ let stack = Object . values ( node . children )
50
+ while ( stack . length > 0 ) {
51
+ node = stack . pop ( )
52
+ stack = stack . concat ( Object . values ( node . children ) )
53
+ sum += node . val ? node . val : 0
54
+ }
55
+
56
+ return sum
57
+ } ;
58
+
59
+ /**
60
+ * Your MapSum object will be instantiated and called as such:
61
+ * var obj = Object.create(MapSum).createNew()
62
+ * obj.insert(key,val)
63
+ * var param_2 = obj.sum(prefix)
64
+ */
Original file line number Diff line number Diff line change
1
+ class MapSum (object ):
2
+
3
+ class TrieNode (object ):
4
+ def __init__ (self , char ):
5
+ self .char = char
6
+ self .children = {}
7
+ self .val = None
8
+
9
+ def __init__ (self ):
10
+ """
11
+ Initialize your data structure here.
12
+ """
13
+ self .root = self .TrieNode (None )
14
+
15
+ def insert (self , key , val ):
16
+ """
17
+ :type key: str
18
+ :type val: int
19
+ :rtype: void
20
+ """
21
+ node = self .root
22
+ for c in key :
23
+ if node .children .get (c ) == None :
24
+ node .children [c ] = self .TrieNode (c )
25
+ node = node .children [c ]
26
+ node .val = val
27
+
28
+ def sum (self , prefix ):
29
+ """
30
+ :type prefix: str
31
+ :rtype: int
32
+ """
33
+ sm = 0
34
+ node = self .root
35
+
36
+ for c in prefix :
37
+ if node .children .get (c ) == None :
38
+ return 0
39
+ node = node .children [c ]
40
+
41
+ sm += node .val if node .val else 0
42
+
43
+ stack = node .children .values ()
44
+ while len (stack ) > 0 :
45
+ node = stack .pop ()
46
+ stack += node .children .values ()
47
+ sm += node .val if node .val else 0
48
+
49
+ # or use recursive
50
+ # for key in node.children:
51
+ # sm += self.sum(prefix + key)
52
+
53
+ return sm
54
+
55
+
56
+ # Your MapSum object will be instantiated and called as such:
57
+ # obj = MapSum()
58
+ # obj.insert(key,val)
59
+ # param_2 = obj.sum(prefix)
You can’t perform that action at this time.
0 commit comments