File tree Expand file tree Collapse file tree 2 files changed +107
-0
lines changed
lcof2/剑指 Offer II 062. 实现前缀树 Expand file tree Collapse file tree 2 files changed +107
-0
lines changed Original file line number Diff line number Diff line change @@ -418,6 +418,62 @@ public class Trie {
418
418
*/
419
419
```
420
420
421
+ #### Swift
422
+
423
+ ``` swift
424
+ class Trie {
425
+ private var children: [Trie? ]
426
+ private var isEnd: Bool
427
+
428
+ init () {
429
+ self .children = Array (repeating : nil , count : 26 )
430
+ self .isEnd = false
431
+ }
432
+
433
+ func insert (_ word : String ) {
434
+ var node = self
435
+ for char in word {
436
+ let index = Int (char.asciiValue ! - Character (" a" ).asciiValue ! )
437
+ if node.children[index] == nil {
438
+ node.children [index] = Trie ()
439
+ }
440
+ node = node.children [index]!
441
+ }
442
+ node.isEnd = true
443
+ }
444
+
445
+ func search (_ word : String ) -> Bool {
446
+ if let node = searchPrefix (word) {
447
+ return node.isEnd
448
+ }
449
+ return false
450
+ }
451
+
452
+ func startsWith (_ prefix : String ) -> Bool {
453
+ return searchPrefix (prefix ) != nil
454
+ }
455
+
456
+ private func searchPrefix (_ prefix : String ) -> Trie? {
457
+ var node = self
458
+ for char in prefix {
459
+ let index = Int (char.asciiValue ! - Character (" a" ).asciiValue ! )
460
+ if node.children[index] == nil {
461
+ return nil
462
+ }
463
+ node = node.children [index]!
464
+ }
465
+ return node
466
+ }
467
+ }
468
+ /**
469
+ * Your Trie object will be instantiated and called as such:
470
+ * let trie = Trie()
471
+ * trie.insert(word);
472
+ * trie.search(word);
473
+ * trie.startsWith(prefix);
474
+ */
475
+ ```
476
+
421
477
<!-- tabs:end -->
422
478
423
479
<!-- solution:end -->
Original file line number Diff line number Diff line change
1
+ class Trie {
2
+ private var children : [ Trie ? ]
3
+ private var isEnd : Bool
4
+
5
+ init ( ) {
6
+ self . children = Array ( repeating: nil , count: 26 )
7
+ self . isEnd = false
8
+ }
9
+
10
+ func insert( _ word: String ) {
11
+ var node = self
12
+ for char in word {
13
+ let index = Int ( char. asciiValue! - Character( " a " ) . asciiValue!)
14
+ if node. children [ index] == nil {
15
+ node. children [ index] = Trie ( )
16
+ }
17
+ node = node. children [ index] !
18
+ }
19
+ node. isEnd = true
20
+ }
21
+
22
+ func search( _ word: String ) -> Bool {
23
+ if let node = searchPrefix ( word) {
24
+ return node. isEnd
25
+ }
26
+ return false
27
+ }
28
+
29
+ func startsWith( _ prefix: String ) -> Bool {
30
+ return searchPrefix ( prefix) != nil
31
+ }
32
+
33
+ private func searchPrefix( _ prefix: String ) -> Trie ? {
34
+ var node = self
35
+ for char in prefix {
36
+ let index = Int ( char. asciiValue! - Character( " a " ) . asciiValue!)
37
+ if node. children [ index] == nil {
38
+ return nil
39
+ }
40
+ node = node. children [ index] !
41
+ }
42
+ return node
43
+ }
44
+ }
45
+ /**
46
+ * Your Trie object will be instantiated and called as such:
47
+ * let trie = Trie()
48
+ * trie.insert(word);
49
+ * trie.search(word);
50
+ * trie.startsWith(prefix);
51
+ */
You can’t perform that action at this time.
0 commit comments