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 af32277

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent fde0504 commit af32277

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

‎146_lru_cache/lru_cache.cc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class LRUCache {
1515
}
1616

1717
int get(int key) {
18-
if (ht_.find(key) == ht_.end()) {
18+
if (ht_.count(key) == 0) {
1919
return -1;
2020
}
2121

@@ -34,7 +34,7 @@ class LRUCache {
3434
return;
3535
}
3636

37-
if (ht_.find(key) != ht_.end()) {
37+
if (ht_.count(key) == 0) {
3838
li_.erase(ht_[key]);
3939
} else {
4040
if (li_.size() == cap_) {

‎460_lfu_cache/lfu_cache.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ LFUCache* lFUCacheCreate(int capacity)
344344

345345
int lFUCacheGet(LFUCache* obj, int key)
346346
{
347+
if (obj->capacity <= 0) {
348+
return;
349+
}
350+
347351
struct list_head *pos;
348352
int hash = key % obj->capacity;
349353
list_for_each(pos, &obj->hheads[hash]) {

‎460_lfu_cache/lfu_cache.cc‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class LFUCache {
1919
}
2020

2121
int get(int key) {
22-
if (key_map_.find(key) == key_map_.end()) {
22+
if (key_map_.count(key) == 0) {
2323
return -1;
2424
}
2525

@@ -32,7 +32,7 @@ class LFUCache {
3232
return;
3333
}
3434

35-
if (key_map_.find(key) != key_map_.end()) {
35+
if (key_map_.count(key) == 0) {
3636
freq_incr(key);
3737
(*key_map_[key])->value_ = value;
3838
} else {
@@ -63,7 +63,7 @@ class LFUCache {
6363
}
6464

6565
void _freq_incr(int key, int value, int freq) {
66-
if (freq_map_.find(freq + 1) == freq_map_.end()) {
66+
if (freq_map_.count(freq + 1) == 0) {
6767
freq_map_[freq + 1] = li_;
6868
}
6969

0 commit comments

Comments
(0)

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