騰訊微信安卓開發(fā)一面面經(jīng)
- 介紹自己的項目,問了項目的難點和創(chuàng)新點
2. 為什么算法轉(zhuǎn)開發(fā)
3. 介紹C++智能指針
4. 下面兩個樣例的對象a有什么區(qū)別
(1)
Person a();
(2)
Person a;
a = Person();
5. coding
設(shè)置一個算法,有一下功能
1.LFUCache(int capacity) :用數(shù)據(jù)結(jié)構(gòu)的容量 capacity 初始化對象
http://2.int?get(int key) : 如果鍵 key 存在于緩存中,則獲取鍵的值,否則返回 -1 。
3.void put(int key, int value) : 如果鍵 key 已存在,則變更其值;如果鍵不存在,請插入鍵值對。當(dāng)緩存達(dá)到其容量 capacity 時,則應(yīng)該在插入新項之前,移除最不經(jīng)常使用的項。在此問題中,當(dāng)存在平局(即兩個或更多個鍵具有相同使用頻率)時,應(yīng)該去除 最近最久未使用 的鍵。
為了確定最不常使用的鍵,可以為緩存中的每個鍵維護(hù)一個 使用計數(shù)器 。使用計數(shù)最小的鍵是最久未使用的鍵。
當(dāng)一個鍵首次插入到緩存中時,它的使用計數(shù)器被設(shè)置為 1 (由于 put 操作)。對緩存中的鍵執(zhí)行 get 或 put 操作,使用計數(shù)器的值將會遞增。
#include <iostream> #include <map> #include <cstdio> #include <vector> using namespace std; class LFUCache { private: int capacity; map<int , int> cache, count; public: LFUCache(int capacity1): capacity(capacity1) {} int get(int key) { if(cache.count(key) == 1) { count[key] += 1; return cache[key]; } return -1; } void put(int key, int value) { int min = 1e9, index = -1; if(cache.size() == capacity) { for(map<int, int>::iterator it = count.begin(); it != count.end(); ++it) { if(it->second < min) { min = it->second; index = it->first; } } cache.erase(index); } cache[key] = value; count[key] += 1; } }; int main() { LFUCache cache(3); cout << cache.get(0) << endl; int a[5] = {1, 2, 4, 3, 4}; for(int i = 0; i < 5; ++i) { cache.put(a[i], i); } // 2 1 // 4 4 // 3 3 // for(int i = 0; i < 5; ++i) // { // cout << cache.get(a[i]) << endl; // } cout << cache.get(1) << endl; cout << cache.get(2) << endl; cout << cache.get(3) << endl; cout << cache.get(4) << endl; return 0; }#騰訊##微信##秋招##面經(jīng)##安卓#