Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 13706a5

Browse files
feat: add ts solution to lc problem: No.0146 (doocs#1670)
1 parent aed89ac commit 13706a5

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

‎solution/0100-0199/0146.LRU Cache/README.md‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,44 @@ private:
568568
*/
569569
```
570570
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+
571609
### **...**
572610

573611
```

‎solution/0100-0199/0146.LRU Cache/README_EN.md‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,44 @@ private:
516516
*/
517517
```
518518
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+
519557
### **...**
520558

521559
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /