20道题目+3编程题
1. 统计数字二进制中1的个数
#include <iostream>
using namespace std;
int main()
{
long a;
while (cin >> a)//注意while处理多个case
{
int c = 0;
for (; a; ++c)
{
a &= (a - 1); // 清除最低位的1
}
cout << c<< endl;
}
return 0;
}
2.查表算法logn(我这不是这个复杂度)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
long count = 0; // 数据数量
long time = 0;
cin >> count;
cin >> time;
vector<vector<long>> data;
//输入数据
for (int i = 0; i < count; i++)
{
vector<long> temp;
long t = 0;
cin >> t;
temp.push_back(t);
cin >> t;
temp.push_back(t);
cin >> t;
temp.push_back(t);
data.push_back(temp);
}
vector<long> result;
//判断
for (vector<vector<long>>::iterator it = data.begin(); it != data.end(); it++)
{
if ((*it)[1] > time) continue;
if ((*it)[2] < time) continue;
result.push_back((*it)[0]);
}
//输出
if (result.size() == 0)
{
cout << "null";
}
else
{
sort(result.begin(), result.end());
for (int i = 0; i < result.size(); i++)
{
cout << result[i] << endl;
}
}
return 0;
}
3. 这个就有点皮了
设计并实现一个LRU Cache。
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
// 双向链表的节点结构
struct LRUNode
{
int key;
int value;
LRUNode* prev;
LRUNode* next;
LRUNode() :key(0), value(0), prev(NULL), next(NULL) {}
};
class LRU
{
private:
unordered_map<int, LRUNode*> m; // 代替hash_map
LRUNode* head; // 指向双链表的头结点
LRUNode* tail; // 指向双链表的尾结点
int capacity; // Cache的容量
int count; // 计数
public:
LRU(int capacity); // 构造函数
~LRU(); // 析构函数
int get(int key); // 查询数据项
void put(int key, int value); // 未满时插入,已满时替换
private:
void removeLRUNode(); // 删除尾结点(最久未使用)
void detachNode(LRUNode* node); // 分离当前结点
void insertToFront(LRUNode* node); // 节点插入到头部
};
LRU::LRU(int capacity)
{
this->capacity = capacity;
this->count = 0;
head = new LRUNode;
tail = new LRUNode;
head->prev = NULL;
head->next = tail;
tail->prev = head;
tail->next = NULL;
}
LRU::~LRU()
{
delete head;
delete tail;
}
int LRU::get(int key)
{
if (m.find(key) == m.end()) // 没找到
return -1;
else
{
LRUNode* node = m[key];
detachNode(node); // 命中,移至头部
insertToFront(node);
return node->value;
}
}
void LRU::put(int key, int value)
{
if (m.find(key) == m.end()) // 没找到
{
LRUNode* node = new LRUNode;
if (count == capacity) // Cache已满
removeLRUNode();
node->key = key;
node->value = value;
m[key] = node; // 插入哈希表
insertToFront(node); // 插入链表头部
++count;
}
else
{
LRUNode* node = m[key];
detachNode(node);
node->value = value;
insertToFront(node);
}
}
void LRU::removeLRUNode()
{
LRUNode* node = tail->prev;
detachNode(node);
m.erase(node->key);
--count;
}
void LRU::detachNode(LRUNode* node)
{
node->prev->next = node->next;
node->next->prev = node->prev;
}
void LRU::insertToFront(LRUNode* node)
{
node->next = head->next;
node->prev = head;
head->next = node;
node->next->prev = node;
}
int main()
{
int count = 0;
cin >> count;
LRU data(count);
vector<int> result; //输出的结果
char index = ' ';
while (cin >> index)
{
if (index == 'p')
{
int a = 0, b = 0;
cin >> a >> b;
//result.push_back(data.put(a, b));
data.put(a, b);
}
if (index == 'g')
{
int a = 0;
cin >> a;
result.push_back(data.get(a));
}
}
for (int i = 0; i < result.size(); i++)
{
cout << result[i] << endl;
}
return 0;
}
参考: 设计并实现一个LRU Cache