数据结构与算法分析 c++11 练习3.4 3.5 给定两个已排序的表L1和L2,求交集和并集
来源:互联网 发布:浙江省网络作协扶持 编辑:程序博客网 时间:2024/05/28 15:56
这个小问题没怎么考虑效率,用List标准库的函数实现下吧。
#include <iostream>#include <list> using namespace std; void printList(list<int> t){auto itr = t.begin();while (itr!=t.end()){ cout << *itr++<<" "; }cout << endl;} template <typename T>list<T> intersectionList(list<T> &lhs, list<T> &rhs){list<T> itrsectLst;auto l1 = lhs.begin();for (; l1 != lhs.end(); l1++){auto r2 = rhs.begin();if (*l1 < *r2 || *l1 > *(--rhs.end()))continue;else {for (; r2 != rhs.end(); r2++){if (*l1 == *r2)itrsectLst.push_back(*l1);}}}itrsectLst.sort();itrsectLst.unique();return itrsectLst;}template <typename T>list<T> unionList(list<T> &lhs, list<T> &rhs){list<T> unionLst=lhs,temLst=rhs;unionLst.sort();temLst.sort();unionLst.merge(temLst);unionLst.unique();return unionLst;}int main(){ list<int> lbt= { 15,12,0, 2, 3, 5, 6, 7, 9, 12 }, rbt={ 1, 2, 4, 5, 7, 8, 9,12, 13 };list<int> itslst,unionlst;printList(lbt);printList(rbt);cout << "test intersection: " << endl;itslst=intersectionList(lbt,rbt);printList(itslst);cout << "test union: " << endl;unionlst = unionList(lbt, rbt);printList(unionlst);}
0 0
- 数据结构与算法分析 c++11 练习3.4 3.5 给定两个已排序的表L1和L2,求交集和并集
- 给定两个排序后的链表L1\L2,仅用基本表操作求链表的交集和并集
- 给定两个已排序的表L1和L2 只使用基本的表操作编写计算L1∩L2的过程 交集
- 给定两个已排序的表L1和L2 只使用基本的表操作编写计算L1∪L2的过程 并集
- 单项链表 练习3.4 给定两个已排序的表求交集
- 算法5:求两个已排序数组的交集和并集
- 单项链表 练习3.5 给定两个已排序的表求并集
- 算法兴趣-----求两个已排序数组的交集和并集(算法复杂度O(N+M))
- 求两个数组的交集、并集和差集算法分析与实现
- 求两个数组的交集、并集和差集算法分析与实现
- 求两个数组的交集、并集和差集算法分析与实现
- 求两个数组的交集、并集和差集算法分析与实现
- 给定两个排序后的表,用C++求其交集和并集
- 两个排序后的表L1和L2的交和并
- python 线性表L1和L2求并集
- 仅使用基本的表操作实现两个排序后的表L1和L2的并集
- 求两个链表的并集和交集
- 求两个链表的并集和交集
- linux红帽系统管理:DNS服务
- CString,string,char数组的转换
- 35. Search Insert Position
- Relief RCNN: UtilizingConvolutionalFeatureInterrelationshipforFastObject DetectionDeployment阅读笔记
- removeElement
- 数据结构与算法分析 c++11 练习3.4 3.5 给定两个已排序的表L1和L2,求交集和并集
- React Native ES6写法总结
- 替换JFrame窗口左上角图标的方法
- java String类型的处理
- selenium + phantomjs+python 外网动态爬虫
- linux定时任务的设置
- 经典排序——快速排序——C语言版
- 【转载】----C++加密解密库之选择
- jquery中e.target是什么意思