File tree Expand file tree Collapse file tree 3 files changed +109
-0
lines changed
solution/0100-0199/0146.LRU Cache Expand file tree Collapse file tree 3 files changed +109
-0
lines changed Original file line number Diff line number Diff line change @@ -568,6 +568,44 @@ private:
568
568
* /
569
569
```
570
570
571
+ ### **TypeScript**
572
+
573
+ ```ts
574
+ class LRUCache {
575
+ capacity: number;
576
+ map: Map<number, number>;
577
+ constructor(capacity: number) {
578
+ this.capacity = capacity;
579
+ this.map = new Map();
580
+ }
581
+
582
+ get(key: number): number {
583
+ if (this.map.has(key)) {
584
+ const val = this.map.get(key)!;
585
+ this.map.delete(key);
586
+ this.map.set(key, val);
587
+ return val;
588
+ }
589
+ return -1;
590
+ }
591
+
592
+ put(key: number, value: number): void {
593
+ this.map.delete(key);
594
+ this.map.set(key, value);
595
+ if (this.map.size > this.capacity) {
596
+ this.map.delete(this.map.keys().next().value);
597
+ }
598
+ }
599
+ }
600
+
601
+ /**
602
+ * Your LRUCache object will be instantiated and called as such:
603
+ * var obj = new LRUCache(capacity)
604
+ * var param_1 = obj.get(key)
605
+ * obj.put(key,value)
606
+ */
607
+ ```
608
+
571
609
### ** ...**
572
610
573
611
```
Original file line number Diff line number Diff line change @@ -516,6 +516,44 @@ private:
516
516
* /
517
517
```
518
518
519
+ ### **TypeScript**
520
+
521
+ ```ts
522
+ class LRUCache {
523
+ capacity: number;
524
+ map: Map<number, number>;
525
+ constructor(capacity: number) {
526
+ this.capacity = capacity;
527
+ this.map = new Map();
528
+ }
529
+
530
+ get(key: number): number {
531
+ if (this.map.has(key)) {
532
+ const val = this.map.get(key)!;
533
+ this.map.delete(key);
534
+ this.map.set(key, val);
535
+ return val;
536
+ }
537
+ return -1;
538
+ }
539
+
540
+ put(key: number, value: number): void {
541
+ this.map.delete(key);
542
+ this.map.set(key, value);
543
+ if (this.map.size > this.capacity) {
544
+ this.map.delete(this.map.keys().next().value);
545
+ }
546
+ }
547
+ }
548
+
549
+ /**
550
+ * Your LRUCache object will be instantiated and called as such:
551
+ * var obj = new LRUCache(capacity)
552
+ * var param_1 = obj.get(key)
553
+ * obj.put(key,value)
554
+ */
555
+ ```
556
+
519
557
### ** ...**
520
558
521
559
```
Original file line number Diff line number Diff line change
1
+ class LRUCache {
2
+ capacity : number ;
3
+ map : Map < number , number > ;
4
+ constructor ( capacity : number ) {
5
+ this . capacity = capacity ;
6
+ this . map = new Map ( ) ;
7
+ }
8
+
9
+ get ( key : number ) : number {
10
+ if ( this . map . has ( key ) ) {
11
+ const val = this . map . get ( key ) ! ;
12
+ this . map . delete ( key ) ;
13
+ this . map . set ( key , val ) ;
14
+ return val ;
15
+ }
16
+ return - 1 ;
17
+ }
18
+
19
+ put ( key : number , value : number ) : void {
20
+ this . map . delete ( key ) ;
21
+ this . map . set ( key , value ) ;
22
+ if ( this . map . size > this . capacity ) {
23
+ this . map . delete ( this . map . keys ( ) . next ( ) . value ) ;
24
+ }
25
+ }
26
+ }
27
+
28
+ /**
29
+ * Your LRUCache object will be instantiated and called as such:
30
+ * var obj = new LRUCache(capacity)
31
+ * var param_1 = obj.get(key)
32
+ * obj.put(key,value)
33
+ */
You can’t perform that action at this time.
0 commit comments