-- 克隆
function Clone(object)
local lookup_table = { }
local function _copy(object)
if type(object) ~= "table" then
return object
elseif lookup_table[object] then
return lookup_table[object]
end
local new_table = { }
lookup_table[object] = new_table
for key, value in pairs(object) do
new_table[_copy(key)] = _copy(value)
end
return setmetatable(new_table, getmetatable(object))
end
return _copy(object)
end -- 合并
function Merge(...)
local arrays = { ... }
local result = {}
for _,array in ipairs(arrays) do
for _, v in ipairs(array) do
table.insert(result, v)
end
end return result
end -- 交集
function Intersection(t1, t2)
local ret = {}
for k, v1 in pairs(t1) do
local equal = false
for k, v2 in pairs(t2) do
if v1 == v2 then
equal = true
break
end
end
if equal then
table.insert(ret, v1)
end
end
return ret
end -- 补集
function Complement(t1, t2)
-- 在这个函数调用多次并且是同一个数组时, 如果不写标号1这行, 下边标号2和标号3的元素移除会对传过来的数组有污染。
local t1, t2 = Clone(t1), Clone(t2) -- 标号1
for i = #t1, , - do
for j = #t2, , - do
if t1[i] == t2[j] then
table.remove(t1, i) -- 标号2
table.remove(t2, j) -- 标号3
end
end
end if #t1 ~= then
return t1
else
return t2
end
end -- 并集
function Aggregate(t1, t2)
local ret = Merge(t1, t2)
local t = {}
for _, v1 in pairs(ret) do
local exist = false
for _, v2 in pairs(t) do
if v1 == v2 then
exist = true
end
end
if not exist then
table.insert(t, v1)
end
end
return t
end local t1 = {, , , , , }
local t2 = {, , , }
local t3 = {, , , } -- 补集专用 -- 并集
print("-------------并集--------------")
local agg = Aggregate(t1, t2)
for k,v in pairs(agg) do
print(k,v)
end print("-------------分割线--------------")
print("") -- 补集
print("-------------补集--------------")
local com = Complement(t1, t3)
for k,v in pairs(com) do
print(k,v)
end print("-------------分割线--------------")
print("") -- 交集
print("-------------交集--------------")
local inter = Intersection(t1, t2)
for k,v in pairs(inter) do
print(k,v)
end

用lua求两个数组的交集、并集和补集。的更多相关文章

  1. java用最少循环求两个数组的交集、差集、并集

    import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List ...

  2. java使用bitmap求两个数组的交集

    一般来说int代表一个数字,但是如果利用每一个位 ,则可以表示32个数字 ,在数据量极大的情况下可以显著的减轻内存的负担.我们就以int为例构造一个bitmap,并使用其来解决一个简单的问题:求两个数 ...

  3. js取两个数组的交集|差集|并集|补集|去重示例代码

    http://www.jb51.net/article/40385.htm 代码如下: /** * each是一个集合迭代函数,它接受一个函数作为参数和一组可选的参数 * 这个迭代函数依次将集合的每一 ...

  4. leetcode-350-Intersection of Two Arrays II(求两个数组的交集)

    题目描述: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, ...

  5. (C#) 求两个数组的交集

    基本上在面试的时候,会具体到两个int数组,或string数组.具体也就是讨论算法. 首先需要的是和面试的人确认题目的含义,并非直接答题. 然后,可以提出自己的想法,首先最快的是用linq { Lis ...

  6. js求两个数组的交集|并集|差集|去重

    let a = [1,2,3], b= [2, 4, 5]; 1.差集 (a-b 差集:属于a但不属于b的集合)  a-b = [1,3] (b-a 差集:属于b但不属于a的集合)  b-a = [4 ...

  7. 求两个集合的交集和并集C#

    我是用hashset<T>来实现的 具体如代码所示 using System; using System.Collections.Generic; using System.Linq; u ...

  8. VBA/Excel-实例系列-04-求两个数组的交集

    原创: Z Excel高效办公之VBA 2017-03-10 Part 1:逻辑过程 已有两个数组,要求单个数组中信息无重复 以最短的数组作为循环,分别判断该数组中的元素是否在另一个数组中 如果某一元 ...

  9. LeetCode初级算法之数组:350 两个数组的交集 II

    两个数组的交集 II 题目地址:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/ 给定两个数组,编写一个函数来计算它们的交 ...

随机推荐

  1. linux 去掉 ^M 的方法

    在linux上经常遇到这种问题,从网上下载文件到 linux 上后,就多了很多 ^M这种东西,如何集体删除这种东西呢! 用 vim 打开文件 进行如下设置  将文件格式转化为unix :set ff= ...

  2. Xor-matic Number of the Graph-CodeForces - 724G

    Xor-matic Number of the Graph-CodeForces - 724G 线性基棒题 建议做这题前先看看线性基的概念,然后A掉这道题--->路径最大异或和 这两个题都用到了 ...

  3. [技术博客] 数据库1+N查询问题

    目录 问题简述 问题解决 group的方法简化查询 改正后的代码 作者:庄廓然 问题简述 本次开发过程中我们用到了rails的orm框架,使用orm框架可以很方便地进行对象的关联和查询,例如查询一个用 ...

  4. ios 报错 Invalid row height provided by table delegate. Value must be at least 0.0, or UITableViewAutomaticDi......

    Invalid row height provided by table delegate. Value must be at least 0.0, or UITableViewAutomaticDi ...

  5. apache Request-URI Too Large 处理办法

    在Apache的httpd.conf配置文件中(直接加就可以) LimitRequestLine 40940 LimitRequestFieldSize 40940

  6. Mac下iTerm2使用

    之前一直使用 Mac OS 自带的终端,用起来虽然有些不太方便,但总体来说还是可以接受的,是有想换个终端的想法,然后今天偶然看到一个终端利器 iTerm2,发现真的很强大,也非常的好用,按照网上配置了 ...

  7. DOS批处理中%~dp0表示什么意思

    https://www.jianshu.com/p/5a1a882ead95 https://www.cnblogs.com/cnpirate/p/5282324.html https://www.c ...

  8. django中安全sql注入等

    模拟sql注入 使用原生sql语句编写login登录逻辑 class LoginUnsafeView(View): def get(self, request): return render(requ ...

  9. golang教材

    https://golangbot.com/buffered-channels-worker-pools/ https://golang.org/doc/ https://medium.com/go- ...

  10. MSSQL Server 及 MSSQL Express版本 自动备份

    一.SQL Server Management Studio(SMSS) 维护计划 [参考]SQL SERVER如何定期自动备份数据库 二.Windows 级 任务计划程序( MSSQL Expres ...