C++: 使用红黑树模拟实现STL中的map和set

news2024/9/20 17:52:38

目录

  • 1. 红黑树的迭代器
    • ++和--
  • 2. 改造红黑树
  • 3. set的模拟实现
  • 4. map的模拟实现
  • 5. RBTree的改造代码

博客主页 : 酷酷学


正文开始

1. 红黑树的迭代器

迭代器的好处是可以方便遍历,是数据结构的底层实现与用户透明

打开C++的源码我们可以发现, 其实源码中的底层大概如下图所示:

在这里插入图片描述

这里额外增加了一个header指针, 有了这个指针可以更方便的找到根节点, 并且可以比较容易的实现反向遍历, 可以看到set和map都是双向迭代器, 但是缺点就是需要不断的维护begin()这个要返回的节点, 所以我们这里为了也是先正反向迭代器, 也避免过于麻烦, 我们暂且讲_root也一并传过来, 方便我们找到根节点

template<class T,class Ref,class Ptr>
struct RBTreeIterator
{
	typedef RBTreeNode<T> Node;
	typedef RBTreeIterator<T,Ref,Ptr> self;

	Node* _node;
	Node* _root;

	RBTreeIterator(Node* node,Node* root)
		:_node(node)
		, _root(root)
	{}

	Ref operator*()
	{
		return _node->_data;
	}

	Ptr operator->()
	{
		return &_node->_data;
	}

	bool operator==(const self& s)
	{
		return _node == s._node;
	}

	bool operator!=(const self& s)
	{
		return _node != s._node;
	}
};

在BSTree中, 有了模板Ref, 和Ptr当我们需要const迭代器就比较方便

	typedef RBTreeIterator<T,T&,T*> Iterator;
	typedef RBTreeIterator<T, const T&, const T*> ConstIterator;

	Iterator Begin()
	{
		Node* leftMost = _root;
		while (leftMost && leftMost->_left)
		{
			leftMost = leftMost->_left;
		}
		return Iterator(leftMost,_root);
	}

	Iterator End()
	{
		return Iterator(nullptr,_root);
	}

	ConstIterator Begin() const
	{
		Node* leftMost = _root;
		while (leftMost && leftMost->_left)
		{
			leftMost = leftMost->_left;
		}
		return ConstIterator(leftMost, _root);
	}

	ConstIterator End() const
	{
		return ConstIterator(nullptr,_root);
	}

++和–

对于++,我们由局部到整体分析,首先如果右不为空, 那么我们要访问下一个节点就是右子树的最左节点.

在这里插入图片描述

如果右为空, 我们就需要访问孩子是父亲左的那个祖先,因为中序的遍历的顺序为左 根 右,当前节点访问完了, 说明我这棵树的左根右访问完了, 要去访问上一棵树的根.
在这里插入图片描述

	self& operator++()
	{
		if (_node->_right)
		{
			//右不为空, 右子树的最左节点就是中序的下一个
			Node* leftMost = _node->_right;
			while (leftMost->_left)
			{
				leftMost = leftMost->_left;
			}
			_node = leftMost;
		}
		else
		{
			//沿着到根节点的路径查找,孩子是父亲左的那个祖先
			//节点就是下一个要访问的节点
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_right)
			{
				cur = parent;
				parent = cur->_parent;
			}
			_node = parent;
		}
		return *this;
	}

对于–操作,就是按照右 根 左的操作, 和++反着来, 因为我们要模拟实现反向迭代, 所以当节点为空时,也就是end()时, 我们–之后要返回到最后一个节点

	self& operator--()
	{
		if (_node == nullptr)
		{
			//--end()
			Node* rightMost = _root;
			while (rightMost && rightMost->_right)
			{
				rightMost = rightMost->_right;
			}
			_node = rightMost;
		}
		else if (_node->_left)
		{
			Node* rightMost = _node->_left;
			while (rightMost->_right)
			{
				rightMost = rightMost->_right;
			}
			_node = rightMost;
		}
		else
		{
			//找孩子是右的那个父亲
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_left)
			{
				cur = parent;
				parent = cur->_parent;
			}
			_node = parent;
		}
		return *this;
	}

2. 改造红黑树

对于map和set底层存放的一个是key,一个是key_value, 难道我们需要为此适配不同的红黑树吗, 其实不是, 我们来看一下源码. 这里不管是map还是set都是同一棵树, 只是对它进行了改造.
这里的key就是key, 而value_type如果是set就是key, 如果是map就是pair. 那么为什么不省略第一个key, 既然存在即合理, 因为对于set来说无所谓, 但是对于map来说呢, 如果只有pair, 那么怎么比较呢? 我们需要的比较方式是按照pair中的key来比较, 但是pair的底层比较方法并不是, 还有关于find函数, 我们实现查找难道要传递一个pair查找吗, 那如何实现英汉互译那种场景呢?既然知道pair了那还有什么必要查找呢?

在这里插入图片描述
C++STL底层pair的比较方法
在这里插入图片描述

所以我们进行改造, 统一讲key和pair改为模板T
在这里插入图片描述

template<class K,class T,class KeyOfT>
class RBTree
{
	typedef RBTreeNode<T> Node;
public:
	typedef RBTreeIterator<T,T&,T*> Iterator;
	typedef RBTreeIterator<T, const T&, const T*> ConstIterator;

	pair<Iterator,bool> Insert(const T& data)
	{
		//
		KeyOfT kot;
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (kot(cur->_data) < kot(data))
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (kot(cur->_data) > kot(data))
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return make_pair(Iterator(cur, _root), false);
			}
		}
	}

	Iterator Find(const K& key)
	{
	//
	}
private:
	Node* _root = nullptr;
};

对于比较逻辑, 我们可以创建一个仿函数进行实现

map:

	template<class K,class V>
	class map
	{
		struct MapKeyOfT
		{
			const K& operator()(const pair<K, V>& kv) 
			{
				return kv.first;
			}
		};

set:

	template<class K>
	class set
	{
		struct SetKeyOfT
		{
			const K& operator()(const K& key)
			{
				return key;
			}
		};

在这里插入图片描述

3. set的模拟实现

#include"RBTree.h"

namespace my
{
	template<class K>
	class set
	{
		struct SetKeyOfT
		{
			const K& operator()(const K& key)
			{
				return key;
			}
		};
	public:
		typedef typename RBTree<K,const K, SetKeyOfT>::Iterator iterator;
		typedef typename RBTree<K,const K, SetKeyOfT>::Iterator const_iterator;
		iterator begin()
		{
			return _t.Begin();
		}

		iterator end() 
		{
			return _t.End();
		}

		const_iterator begin() const
		{
			return _t.Begin();
		}

		const_iterator end() const
		{
			return _t.End();
		}

		pair<iterator,bool> insert(const K& key)
		{
			return _t.Insert(key);
		} 

		iterator find(const K& key)
		{
			return _t.Find(key);
		}
	private:
		RBTree<K, const K, SetKeyOfT> _t;
	};

	void Print(const set<int>& s)
	{
		set<int>::const_iterator it = s.end();
		while (it != s.begin())
		{
			--it;
			cout << *it << " ";
		}
		cout << endl;
	}
	void test_set()
	{
		set<int> s;
		int a[] = { 4,2,6,1,3,5,15,7,16,14 };
		for (auto e : a)
		{
			s.insert(e);
		}
		for (auto e : s)
		{
			cout << e << " ";
		}

		set<int>::iterator it = s.end();
		while (it != s.begin())
		{
			--it;
			cout << *it << " ";
		}
		cout << endl;

	}

}

在这里插入图片描述

4. map的模拟实现

#include"RBTree.h"

namespace my
{
	template<class K,class V>
	class map
	{
		struct MapKeyOfT
		{
			const K& operator()(const pair<K, V>& kv) 
			{
				return kv.first;
			}
		};
	public:
		typedef typename RBTree<K, pair<const K, V>, MapKeyOfT>::Iterator iterator;
		typedef typename RBTree<K, pair<const K, V>, MapKeyOfT>::ConstIterator const_iterator;
		
		iterator begin()
		{
			return _t.Begin();
		}

		iterator end()
		{
			return _t.End();
		}

		const_iterator begin() const
		{
			return _t.Begin();
		}

		const_iterator end() const
		{
			return _t.End();
		}

		pair<iterator,bool> Insert(const pair<K, V>& kv)
		{
			return _t.Insert(kv);
		}

		iterator find(const K& key)
		{
			return _t.Find(key);
		}

		V& operator[](const K& key)
		{
			pair<iterator,bool> ret = Insert(make_pair(key, V()));
			return ret.first->second;
		}
	private:
		RBTree<K, pair<const K, V>, MapKeyOfT> _t;
	};

	void test_map()
	{
		map<string, string> dict;
		dict.Insert({ "sort","排序" });
		dict.Insert({ "left","左边" });
		dict.Insert({ "right","右边" });
		map<string, string>::iterator it = dict.begin();
		while (it != dict.end())
		{
			cout << it->first<<it->second << endl;
			++it;
		}
		dict["left"] = "左边,剩余";
		dict["insert"];
		dict["string"] = "字符串";
		cout << endl;

		for (const auto& e : dict)
		{
			cout << e.first << e.second << endl;
		}
	}
}

在这里插入图片描述

5. RBTree的改造代码

#pragma once
#include<iostream>
#include<vector>
#include<assert.h>
using namespace std;

enum Colour
{
	RED,
	BLACK
};

template<class T>
struct RBTreeNode
{
	T _data;
	RBTreeNode* _left;
	RBTreeNode* _right;
	RBTreeNode* _parent;
	Colour _col;

	RBTreeNode(const T& data)
		: _data(data)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
	{}

};

template<class T,class Ref,class Ptr>
struct RBTreeIterator
{
	typedef RBTreeNode<T> Node;
	typedef RBTreeIterator<T,Ref,Ptr> self;

	Node* _node;
	Node* _root;

	RBTreeIterator(Node* node,Node* root)
		:_node(node)
		, _root(root)
	{}

	self& operator++()
	{
		if (_node->_right)
		{
			//右不为空, 右子树的最左节点就是中序的下一个
			Node* leftMost = _node->_right;
			while (leftMost->_left)
			{
				leftMost = leftMost->_left;
			}
			_node = leftMost;
		}
		else
		{
			//沿着到根节点的路径查找,孩子是父亲左的那个祖先
			//节点就是下一个要访问的节点
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_right)
			{
				cur = parent;
				parent = cur->_parent;
			}
			_node = parent;
		}
		return *this;
	}

	self& operator--()
	{
		if (_node == nullptr)
		{
			//--end()
			Node* rightMost = _root;
			while (rightMost && rightMost->_right)
			{
				rightMost = rightMost->_right;
			}
			_node = rightMost;
		}
		else if (_node->_left)
		{
			Node* rightMost = _node->_left;
			while (rightMost->_right)
			{
				rightMost = rightMost->_right;
			}
			_node = rightMost;
		}
		else
		{
			//找孩子是右的那个父亲
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_left)
			{
				cur = parent;
				parent = cur->_parent;
			}
			_node = parent;
		}
		return *this;
	}



	Ref operator*()
	{
		return _node->_data;
	}

	Ptr operator->()
	{
		return &_node->_data;
	}

	bool operator==(const self& s)
	{
		return _node == s._node;
	}

	bool operator!=(const self& s)
	{
		return _node != s._node;
	}
};

template<class K,class T,class KeyOfT>
class RBTree
{
	typedef RBTreeNode<T> Node;
public:
	typedef RBTreeIterator<T,T&,T*> Iterator;
	typedef RBTreeIterator<T, const T&, const T*> ConstIterator;

	Iterator Begin()
	{
		Node* leftMost = _root;
		while (leftMost && leftMost->_left)
		{
			leftMost = leftMost->_left;
		}
		return Iterator(leftMost,_root);
	}

	Iterator End()
	{
		return Iterator(nullptr,_root);
	}

	ConstIterator Begin() const
	{
		Node* leftMost = _root;
		while (leftMost && leftMost->_left)
		{
			leftMost = leftMost->_left;
		}
		return ConstIterator(leftMost, _root);
	}

	ConstIterator End() const
	{
		return ConstIterator(nullptr,_root);
	}

	RBTree() = default;

	RBTree(const RBTree& t)
	{
		_root = Copy(t._root);
	}

	RBTree& operator=(RBTree t)
	{
		swap(_root, t._root);
		return *this;
	}

	~RBTree()
	{
		Destroy(_root);
		_root = nullptr;
	}

	pair<Iterator,bool> Insert(const T& data)
	{
		if (_root == nullptr)
		{
			_root = new Node(data);
			_root->_col = BLACK;
			return make_pair(Iterator(_root,_root),true);
		}

		KeyOfT kot;
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (kot(cur->_data) < kot(data))
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (kot(cur->_data) > kot(data))
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return make_pair(Iterator(cur, _root), false);
			}
		}

		cur = new Node(data);
		Node* newnode = cur;
		// 新增节点。颜色红色给红色
		cur->_col = RED;
		if (kot(parent->_data) < kot(data))
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
		cur->_parent = parent;

		while (parent && parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
			//    g
			//  p   u
			if (parent == grandfather->_left)
			{
				Node* uncle = grandfather->_right;
				if (uncle && uncle->_col == RED)
				{
					// u存在且为红 -》变色再继续往上处理
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;

					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					// u存在且为黑或不存在 -》旋转+变色
					if (cur == parent->_left)
					{
						//    g
						//  p   u
						//c
						//单旋
						RotateR(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						//    g
						//  p   u
						//    c
						//双旋
						RotateL(parent);
						RotateR(grandfather);

						cur->_col = BLACK;
						grandfather->_col = RED;
					}

					break;
				}
			}
			else
			{
				//    g
				//  u   p
				Node* uncle = grandfather->_left;
				// 叔叔存在且为红,-》变色即可
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;

					// 继续往上处理
					cur = grandfather;
					parent = cur->_parent;
				}
				else // 叔叔不存在,或者存在且为黑
				{
					// 情况二:叔叔不存在或者存在且为黑
					// 旋转+变色
					//      g
					//   u     p
					//            c
					if (cur == parent->_right)
					{
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						//		g
						//   u     p
						//      c
						RotateR(parent);
						RotateL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
		}

		_root->_col = BLACK;

		return make_pair(Iterator(newnode, _root), true);
	}

	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}

	int Height()
	{
		return _Height(_root);
	}

	int Size()
	{
		return _Size(_root);
	}

	bool IsBalance()
	{
		if (_root == nullptr)
			return true;

		if (_root->_col == RED)
		{
			return false;
		}

		// 参考值
		int refNum = 0;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_col == BLACK)
			{
				++refNum;
			}

			cur = cur->_left;
		}

		return Check(_root, 0, refNum);
	}

	Iterator Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < key)
			{
				cur = cur->_right;
			}
			else if (cur->_kv.first > key)
			{
				cur = cur->_left;
			}
			else
			{
				return Iterator(cur, _root);
			}
		}

		return End();
	}
private:
	bool Check(Node* root, int blackNum, const int refNum)
	{
		if (root == nullptr)
		{
			//cout << blackNum << endl;
			if (refNum != blackNum)
			{
				cout << "存在黑色节点的数量不相等的路径" << endl;
				return false;
			}

			return true;
		}

		if (root->_col == RED && root->_parent->_col == RED)
		{
			cout << root->_kv.first << "存在连续的红色节点" << endl;
			return false;
		}

		if (root->_col == BLACK)
		{
			blackNum++;
		}

		return Check(root->_left, blackNum, refNum)
			&& Check(root->_right, blackNum, refNum);
	}

	int _Size(Node* root)
	{
		return root == nullptr ? 0 : _Size(root->_left) + _Size(root->_right) + 1;
	}

	int _Height(Node* root)
	{
		if (root == nullptr)
			return 0;

		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);

		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}

	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}

		_InOrder(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << endl;
		_InOrder(root->_right);
	}

	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		Node* parentParent = parent->_parent;

		subR->_left = parent;
		parent->_parent = subR;

		if (parentParent == nullptr)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (parent == parentParent->_left)
			{
				parentParent->_left = subR;
			}
			else
			{
				parentParent->_right = subR;
			}

			subR->_parent = parentParent;
		}
	}

	void  RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if (subLR)
			subLR->_parent = parent;

		Node* parentParent = parent->_parent;

		subL->_right = parent;
		parent->_parent = subL;

		if (parentParent == nullptr)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (parent == parentParent->_left)
			{
				parentParent->_left = subL;
			}
			else
			{
				parentParent->_right = subL;
			}

			subL->_parent = parentParent;
		}

	}

	void Destroy(Node* root)
	{
		if (root == nullptr)
			return;

		Destroy(root->_left);
		Destroy(root->_right);
		delete root;
	}

	Node* Copy(Node* root)
	{
		if (root == nullptr)
			return nullptr;

		Node* newRoot = new Node(root->_kv);
		newRoot->_left = Copy(root->_left);
		newRoot->_right = Copy(root->_right);

		return newRoot;
	}
	Node* _root = nullptr;
};

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2149572.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数据结构应试-树和二叉树

1. 2. 结点的度&#xff1a;结点拥有的子树数量称为结点的度 树的度&#xff1a;树内各结点度的最大值&#xff0c;即上图 D 结点的度就是此树的度 叶子&#xff1a;度为 0 的节点称为叶子或终端节点 结点的层次和树的深度 森林&#xff1a;m棵互不相交的树的集合。 3. 为啥…

司南 OpenCompass 九月大语言模型评测榜单启动召集,欢迎新合作厂商申请评测

主要概览 司南 OpenCompass 大语言模型官方自建榜单&#xff08;9 月榜&#xff09;评测拟定于 10 月上旬发布&#xff0c;现诚挚邀请新加入的合作方参与评测。本次评测围绕强化能力维度&#xff0c;全面覆盖语言、推理、知识、代码、数学、指令跟随、智能体等七大关键领域&am…

layui时间选择器选择周 日月季度年

<!-- layui框架样式 --><link type"text/css" href"/static/plugins/layui/css/layui.css" rel"stylesheet" /><!-- layui框架js --><script type"text/javascript" src"/static/plugins/layui/layui.js&qu…

【LeetCode】每日一题 2024_9_20 统计特殊整数(数位 DP)

前言 每天和你一起刷 LeetCode 每日一题~ LeetCode 启动&#xff01; 题目&#xff1a;统计特殊整数 代码与解题思路 func countSpecialNumbers(n int) int { // 今天的题目是数位 DP&#xff0c;我不会做&#xff0c;所以现场学习了一下灵神的数位 DP 模版s : strconv.Itoa…

11个推特大V发文推广的数据分析技巧

社交媒体已经成为了现代社会中不可或缺的一部分&#xff0c;而推特作为其中的重要一员&#xff0c;吸引了许多用户。对于那些成千上万的粉丝拥有者&#xff08;也被称为“大V”&#xff09;&#xff0c;他们能够有效地利用推特平台&#xff0c;推广自己的观点和产品。我们将介绍…

让医院更智慧,让决策更容易

依托数字孪生技术&#xff0c;赋能智慧医院&#xff0c;对使用者和决策者带来了众多的优势。数字孪生技术是将物理实体与数字模型相结合&#xff0c;实现实时监测、仿真预测和智能决策的一种先进技术。在智慧医院中应用数字孪生技术&#xff0c;不仅可以提升医疗服务的质量和效…

阿里云容器服务Kubernetes部署新服务

这里部署的是前端项目 1.登录控制台-选择集群 2.选择无状态-命名空间-使用镜像创建 3.填写相关信息 应用基本信息&#xff1a; 容器配置&#xff1a; 高级配置&#xff1a; 创建成功后就可以通过30006端口访问项目了

XML:DOM4j解析XML

XML简介&#xff1a; 什么是XML&#xff1a;XML 是独立于软件和硬件的信息传输工具。 XML 的设计宗旨是传输数据&#xff0c;而不是显示数据。XML 标签没有被预定义。您需要自行定义标签。XML不会做任何事情&#xff0c;XML被设计用来结构化、存储以及传输信息。 XML可以发明…

再次理解UDP协议

一、再谈端口号 在 TCP / IP 协议中&#xff0c;用 "源 IP", "源端口号", "目的 IP", "目的端口号", "协议号" 这样一个五元组来标识一个通信(可以通过 netstat -n 查看) 我们需要端口号到进程的唯一性&#xff0c;所以一个…

工业控制系统等保2.0定级备案经验分享

工业控制系统和传统IT系统有所差异&#xff0c;须单独划分定级对象 工业控制系统定级时将现场采集/执行、现场控制和过程控制等要素应作为一个整体对象定级&#xff0c;各要素不单独定级&#xff1b;生产管理要素可单独定级。对于大型工业控制系统&#xff0c;可以根据系统功能…

Node-red 某一时间范围内满足条件的数据只返回一次

厂子里有个业务需求增加一段逻辑&#xff0c;根据点位数值&#xff0c;判断是否让mes执行之后的逻辑。 网关采集周期5s/次&#xff0c;及数据上报周期5s/次; iot通过网关写入时间为8s左右&#xff1b; 同类设备共用一条规则链&#xff1b; 想当触发条件时修改”完成上传“不…

SimpleAISearch:C# + DuckDuckGo 实现简单的AI搜索

最近AI搜索很火爆&#xff0c;有Perplexity、秘塔AI、MindSearch、Perplexica、memfree、khoj等等。 在使用大语言模型的过程中&#xff0c;或许你也遇到了这种局限&#xff0c;就是无法获取网上最新的信息&#xff0c;导致回答的内容不是基于最新的信息&#xff0c;为了解决这…

[Linux#55][网络协议] 序列化与反序列化 | TcpCalculate为例

目录 1. 理解协议 1.1 结构化数据的传输 序列化与反序列化 代码感知&#xff1a; Request 类 1. 构造函数 2. 序列化函数&#xff1a;Serialize() 3. 反序列化函数&#xff1a;DeSerialize() 补充 4. 成员变量 Response 类 1. 构造函数 2. 序列化函数&#xff1a;…

力扣 中等 2300.咒语和药水的成功对数

文章目录 题目介绍解法 题目介绍 解法 class Solution {public int[] successfulPairs(int[] spells, int[] potions, long success){Arrays.sort(potions);int n spells.length, m potions.length;int[] pairs new int[n];for (int i 0; i < n; i) {int left 0, righ…

无消息传递的图变换器中的图归纳偏差

人工智能咨询培训老师叶梓 转载标明出处 在处理小规模数据集时&#xff0c;图变换器的性能通常不尽如人意&#xff0c;特别是在需要明显的归纳偏好时。为了引入这些偏好&#xff0c;早期的图变换器一般会利用消息传递组件或位置编码。然而&#xff0c;依赖消息传递的图变换器在…

C# AutoResetEvent ManualResetEvent Mutex 对比

三个函数功能类似&#xff0c;都是线程同步的主要函数。但在使用上有一些差别。 关于代码的使用&#xff0c;帖子很多。形象的用图来描述一下。

【Meta分析】IF=12.1!人工智能预测模型Meta分析怎么做?

预测模型的Meta分析 人工智能&#xff08;AI&#xff09;是计算机科学的一个重要分支&#xff0c;其主要目标是让算法执行通常由人类完成的任务。机器学习是指一组允许算法从数据中学习并自我优化的技术&#xff0c;而无需明确编程。深度学习这一术语常与机器学习互换使用&…

怿星设计分享丨设计师与AI的情感化HMI

在当今科技迅速发展的时代背景下&#xff0c;人机交互&#xff08;HMI&#xff09;的设计正从传统的功能性层面转向更加注重用户体验与情感交流的方向。设计师们不再仅仅关注界面的功能性&#xff0c;而是更加重视如何通过设计传递情感&#xff0c;使用户在使用产品时能够感受到…

EsDA,一站式嵌入式软件

EsDA是一套面向工业智能物联领域的嵌入式系统设计自动化工具集&#xff0c;包含实时操作系统AWorksLP、低代码开发平台AWStudio、资源管理平台AXPI、跨平台GUI引擎AWTK和云服务平台ZWS&#xff0c;旨在提高嵌入式软件开发的效率、性能和可扩展性。 EsDA全称是嵌入式系统设计自动…

回归预测|基于饥饿游戏搜索优化随机森林的数据回归预测Matlab程序HGS-RF 多特征输入单输出 高引用先用先创新

回归预测|基于饥饿游戏搜索优化随机森林的数据回归预测Matlab程序HGS-RF 多特征输入单输出 高引用先用先创新 文章目录 一、基本原理1. 饥饿游戏搜索优化算法&#xff08;HGS&#xff09;简介2. 随机森林&#xff08;RF&#xff09;简介3. HGS-RF回归预测流程1. 初始化2. 随机森…