一些题目的代码

(1)类模板的统一形式:

template <class K, class V>
class Node
{
public:
	Node<K, V>() { cout << "node" << endl; }
	~Node<K, V>() { cout << "~~~node" << endl;}
};

int main()
{
	Node<int, int> *node = new Node<int, int>;
	delete node;
}

(2)LRU缓存

#include "pch.h"
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
//LRU  哈希表+双向链表
//哈希表key:用户的key, value:node节点的引用/内存地址
//双向链表:node类型,记录头节点和尾节点,中间是调整好的
//头部是早的,尾部是晚的
//更新的话,将x断开,挪到最后,头尾指针重新设置。都是不需要遍历的。

template <class K, class V>
class Node
{
public:
	K key;
	V value;
	Node<K, V> *last;
	Node<K, V> *next;
	Node(K key, V value)
	{
		this->key = key;
		this->value = value;
	}
};
//双向链表:
template <class K, class V>
class nodeDequelist {
private:
	Node<K, V> head;
    Node<K, V> tail;
public:
	nodeDequelist()
	{
		this->head = nullptr;
		this->tail = nullptr;
	}
	void addNode(Node<K, V> newNode)
	{
		if (newNode == nullptr)  return;
		if (this->head == nullptr)
		{
			this->head = newNode;
			this->tail = newNode;
		}
		else
		{
			this->tail->next = newNode;
			newNode->last = this->tail;
			this->tail = newNode;
		}
	}
	void moveNodetoTail(Node<K, V> node)
	{
		if (node == this->tail) return;
		if (node.last == nullptr)//头节点
		{
			this->head = node->next;
			this->head->last = nullptr;
		}
		else {
			node->last->next = node->next;
			node->next->last = node->last;
		}
		this->tail->next = node;
		node->last = this->tail;
		this->tail = node;
		this->tail->next = nullptr;
		//node->last = this->tail;
		//node->next = nullptr;
		//this->tail->next = node;
		//this->tail = mode;
	}
	Node<K, V> removeHead()
	{
		if (this->head == nullptr) return nullptr;
		Node<K, V> res = this->head;
		if (this->head == this->tail) 
		{
			this->head = nullptr;
			this->tail = nullptr;
		}
		else {
			
			this->head = res->next;
			res->next = nullptr;
			this->head->last = nullptr;
		}
		return res;
	}
};
template <class K, class V>
class myCache {
private:
	unordered_map<K, V> keyNodeMap;
	nodeDequelist<K, V> nodeList;
	int capacity;
public:
	myCache(int cap)
	{
		this->capacity = cap;
		this->keyNodeMap = new unordered_map<K, V>();
		this->nodeList = new nodeDequelist<K, V>();
	}
	V get(K key)
	{
		if (this->keyNodeMap.find(key) != this->keyNodeMap.end())
		{
			Node<K, V> res = this->keyNodeMap[key];
			this->nodeList.moveNodetoTail(res);
			return res.value;
		}
		return nullptr;
	}
	void removeMostUnused()
	{
		Node<K, V> node = this->nodeList.removeHead();
		this->keyNodeMap.erase(this->keyNodeMap[node.key]);
	}
	void set(K key, V value)
	{
		if (this->keyNodeMap.find(key) != this->keyNodeMap.end())
		{
			Node<K,V> ans=this->keyNodeMap[key];
			ans.value = value;
			this->nodeList.moveNodetoTail(ans);
		}
		else {
			Node<K, V> ans = new Node<K, V>(key, value);

			this->keyNodeMap.insert(key, ans);
			this->nodeList.addNode(ans);
			if (this->capacity + 1 == this->keyNodeMap.size())
			{
				removeMostUnused();
			}
		}
	}
};
int main()
{
	myCache<string,int>  testCache(19);
	testCache.set("A", 1);
	testCache.set("B", 2);
	testCache.set("C", 3);
	cout << testCache.get("B") << endl;
	cout << testCache.get("A") << endl;
	testCache.set("D", 4);
	cout << testCache.get("D") << endl;
	cout << testCache.get("C") << endl;
}

(3)求最大公约数

int gcd(int a, int b) //保证调用的时候,a,b不是0
{
	return (b != 0) ? a : gcd(b, a%b);
	
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值