@@ -348,193 +348,151 @@ public:
348
348
```go
349
349
type Trie struct {
350
350
children map[string]*Trie
351
- isEnd bool
351
+ fid int
352
352
}
353
353
354
354
func newTrie() *Trie {
355
- m := map[string]*Trie{}
356
- return &Trie{children: m}
355
+ return &Trie{map[string]*Trie{}, -1}
357
356
}
358
357
359
- func (this *Trie) insert(w string) {
358
+ func (this *Trie) insert(fid int, f string) {
360
359
node := this
361
- for _, p := range strings.Split(w, "/")[1:] {
360
+ ps := strings.Split(f, "/")
361
+ for _, p := range ps[1:] {
362
362
if _, ok := node.children[p]; !ok {
363
363
node.children[p] = newTrie()
364
364
}
365
- node, _ = node.children[p]
365
+ node = node.children[p]
366
366
}
367
- node.isEnd = true
367
+ node.fid = fid
368
368
}
369
369
370
- func (this *Trie) search(w string) bool {
371
- node := this
372
- for _, p := range strings.Split(w, "/")[1:] {
373
- if _, ok := node.children[p]; !ok {
374
- return false
370
+ func (this *Trie) search() (ans []int) {
371
+ var dfs func(*Trie)
372
+ dfs = func(root *Trie) {
373
+ if root.fid != -1 {
374
+ ans = append(ans, root.fid)
375
+ return
375
376
}
376
- node, _ = node.children[p]
377
- if node.isEnd {
378
- return true
377
+ for _, child := range root.children {
378
+ dfs(child)
379
379
}
380
380
}
381
- return false
381
+ dfs(this)
382
+ return
382
383
}
383
384
384
- func removeSubfolders(folder []string) []string {
385
- sort.Slice(folder, func(i, j int) bool {
386
- return len(strings.Split(folder[i], "/")) < len(strings.Split(folder[j], "/"))
387
- })
385
+ func removeSubfolders(folder []string) (ans []string) {
388
386
trie := newTrie()
389
- var ans []string
390
- for _, v := range folder {
391
- if !trie.search(v) {
392
- trie.insert(v)
393
- ans = append(ans, v)
394
- }
387
+ for i, f := range folder {
388
+ trie.insert(i, f)
395
389
}
396
- return ans
390
+ for _, i := range trie.search() {
391
+ ans = append(ans, folder[i])
392
+ }
393
+ return
397
394
}
398
395
```
399
396
400
397
#### TypeScript
401
398
402
399
```ts
403
- function removeSubfolders(folder: string[]): string[] {
404
- const createTrie = (): T => ({ '#': false, children: {} }) ;
405
- const trie = createTrie() ;
400
+ class Trie {
401
+ children: Record<string, Trie> ;
402
+ fid: number ;
406
403
407
- for (const f of folder) {
408
- const path = f.split('/');
409
- path.shift();
404
+ constructor() {
405
+ this.children = {};
406
+ this.fid = -1;
407
+ }
410
408
411
- let node = trie;
412
- for (const p of path) {
413
- if (!node.children[p]) node.children[p] = createTrie();
409
+ insert(i: number, f: string): void {
410
+ let node: Trie = this;
411
+ const ps = f.split('/');
412
+ for (let j = 1; j < ps.length; ++j) {
413
+ const p = ps[j];
414
+ if (!(p in node.children)) {
415
+ node.children[p] = new Trie();
416
+ }
414
417
node = node.children[p];
415
418
}
416
- node['#'] = true ;
419
+ node.fid = i ;
417
420
}
418
421
419
- const ans: string[] = [];
420
- const dfs = (trie: T, path = '') => {
421
- if (trie['#']) {
422
- ans.push(path);
423
- return;
424
- }
425
-
426
- for (const key in trie.children) {
427
- dfs(trie.children[key], path + '/' + key);
428
- }
429
- };
430
-
431
- dfs(trie);
432
-
433
- return ans;
422
+ search(): number[] {
423
+ const ans: number[] = [];
424
+ const dfs = (root: Trie): void => {
425
+ if (root.fid !== -1) {
426
+ ans.push(root.fid);
427
+ return;
428
+ }
429
+ for (const child of Object.values(root.children)) {
430
+ dfs(child);
431
+ }
432
+ };
433
+ dfs(this);
434
+ return ans;
435
+ }
434
436
}
435
437
436
- type T = {
437
- '#': boolean;
438
- children: Record<string, T>;
439
- };
438
+ function removeSubfolders(folder: string[]): string[] {
439
+ const trie = new Trie();
440
+ for (let i = 0; i < folder.length; ++i) {
441
+ trie.insert(i, folder[i]);
442
+ }
443
+ return trie.search().map(i => folder[i]);
444
+ }
440
445
```
441
446
442
447
#### JavaScript
443
448
444
449
```js
445
- function removeSubfolders(folder) {
446
- const createTrie = () => ({ '#': false, children: {} });
447
- const trie = createTrie();
448
-
449
- for (const f of folder) {
450
- const path = f.split('/');
451
- path.shift();
450
+ class Trie {
451
+ constructor() {
452
+ this.children = {};
453
+ this.fid = -1;
454
+ }
452
455
453
- let node = trie;
454
- for (const p of path) {
455
- if (!node.children[p]) node.children[p] = createTrie();
456
+ insert(i, f) {
457
+ let node = this;
458
+ const ps = f.split('/');
459
+ for (let j = 1; j < ps.length; ++j) {
460
+ const p = ps[j];
461
+ if (!(p in node.children)) {
462
+ node.children[p] = new Trie();
463
+ }
456
464
node = node.children[p];
457
465
}
458
- node['#'] = true ;
466
+ node.fid = i ;
459
467
}
460
468
461
- const ans = [];
462
- const dfs = (trie, path = '') => {
463
- if (trie['#']) {
464
- ans.push(path);
465
- return;
466
- }
467
-
468
- for (const key in trie.children) {
469
- dfs(trie.children[key], path + '/' + key);
470
- }
471
- };
472
-
473
- dfs(trie);
474
-
475
- return ans;
476
- }
477
- ```
478
-
479
- <!-- tabs:end -->
480
-
481
- <!-- solution:end -->
482
-
483
- <!-- solution:start -->
484
-
485
- ### 方法三
486
-
487
- <!-- tabs:start -->
488
-
489
- #### Go
490
-
491
- ```go
492
- type Trie struct {
493
- children map[string]*Trie
494
- fid int
495
- }
496
-
497
- func newTrie() *Trie {
498
- return &Trie{map[string]*Trie{}, -1}
499
- }
500
-
501
- func (this *Trie) insert(fid int, f string) {
502
- node := this
503
- ps := strings.Split(f, "/")
504
- for _, p := range ps[1:] {
505
- if _, ok := node.children[p]; !ok {
506
- node.children[p] = newTrie()
507
- }
508
- node = node.children[p]
509
- }
510
- node.fid = fid
511
- }
512
-
513
- func (this *Trie) search() (ans []int) {
514
- var dfs func(*Trie)
515
- dfs = func(root *Trie) {
516
- if root.fid != -1 {
517
- ans = append(ans, root.fid)
518
- return
519
- }
520
- for _, child := range root.children {
521
- dfs(child)
522
- }
523
- }
524
- dfs(this)
525
- return
469
+ search() {
470
+ const ans = [];
471
+ const dfs = root => {
472
+ if (root.fid !== -1) {
473
+ ans.push(root.fid);
474
+ return;
475
+ }
476
+ for (const child of Object.values(root.children)) {
477
+ dfs(child);
478
+ }
479
+ };
480
+ dfs(this);
481
+ return ans;
482
+ }
526
483
}
527
484
528
- func removeSubfolders(folder []string) (ans []string) {
529
- trie := newTrie()
530
- for i, f := range folder {
531
- trie.insert(i, f)
532
- }
533
- for _, i := range trie.search() {
534
- ans = append(ans, folder[i])
535
- }
536
- return
537
- }
485
+ /**
486
+ * @param {string[]} folder
487
+ * @return {string[]}
488
+ */
489
+ var removeSubfolders = function (folder) {
490
+ const trie = new Trie();
491
+ for (let i = 0; i < folder.length; ++i) {
492
+ trie.insert(i, folder[i]);
493
+ }
494
+ return trie.search().map(i => folder[i]);
495
+ };
538
496
```
539
497
540
498
<!-- tabs:end -->
0 commit comments