--利用lua中的table是哈希表这一点进行计算
function lua_distinct_union (union_t1,union_t2)
if(union_t2==nil) then
union_t2={};
end;
local result={};
for k,v in pairs(union_t1) do
result[v]=v;
end;
for k,v in pairs(union_t2) do
if result[v] ==nil then
result[v]=v;
end;
end;
union_t1=nil;
union_t2=nil;
return result;
end; function lua_distinct (distinct_t)
local result={};
for k,v in pairs(distinct_t) do
if(result[v]==nil) then
result[v]=v;
end;
end;
distinct_t=nil;
return result;
end; function lua_distinct_diff(diff_t1,diff_t2) local result={};
for k,v in pairs(diff_t1) do
if(result[v]==nil) then
result[v]=v;
end;
end; for k,v in pairs(diff_t2) do
if(result[v]~=nil) then
table.remove(result,v);
end;
end;
diff_t1=nil;
diff_t2=nil;
return result;
end; function lua_distinct_inter (inter_t1,inter_t2)
if inter_t1==nil and inter_t2~=nil then
return lua_distinct(inter_t2);
end;
if inter_t1~=nil and inter_t2==nil then
return lua_distinct(inter_t1);
end;
local temp1={};
local temp2={};
local result={};
local num1=;
local num2=;
for k,v in pairs(inter_t1) do
if(temp1[v]==nil) then
temp1[v]=v;
num1=num1+;
end;
end;
for k,v in pairs(inter_t2) do
if(temp2[v]==nil) then
temp2[v]=v;
num2=num2+;
end;
end;
inter_t1=nil;
inter_t2=nil;
if num1<num2 then
for k,v in pairs(temp1) do
if temp2[k]~=nil then
result[k]=temp1[k];
end;
end;
else
for k,v in pairs(temp2) do
if temp2[k]~=nil then
result[k]=temp1[k];
end;
end;
end;
temp1=nil;
temp2=nil;
return result;
end; function lua_distinct_union_multiply(tablesneedunion)
if(tablesneedunion==nil or #(tablesneedunion)==) then
error('lua_distinct_diff_Multiply传入参数为空');
end;
local result=tablesneedunion[];
if #(tablesneedunion)== then
return lua_distinct_union(result,nil);
end;
for i=,#(tablesneedunion) do
local tmp=tablesneedunion[i];
result=lua_distinct_union(tmp,result);
end;
tablesneedunion=nil;
return result;
end; function lua_distinct_inter_multiply(tablesneedinter)
if(tablesneedinter==nil or #(tablesneedinter)==) then
error('lua_distinct_inter_multiply传入参数为空');
end;
local result=tablesneedinter[];
if #(tablesneedinter)== then
return lua_distinct_inter(result,nil);
end;
for i=,#(tablesneedinter) do
local tmp=tablesneedinter[i];
result=lua_distinct_inter(tmp,result);
if #(result)== then
break;
end;
end;
tablesneedinter=nil;
return result;
end; function getidsfromkeyvaluepair(keyvaluepair)
local result={};
if(keyvaluepair==nil) then
return result;
end;
local num=;
for k,v in pairs(keyvaluepair) do
result[num]=v;
num=num+;
end;
return result;
end; function lua_getfinalids(structs)
if #(structs)== then
return getidsfromkeyvaluepair(temptable);
end;
local temptable=structs[].temptable;
for i=,#(structs) do
local tmp2=structs[i].temptable;
if structs[i-].flag== then
temptable=lua_distinct_union(temptable,tmp2);
else
temptable=lua_distinct_inter(temptable,tmp2);
end;
end;
return getidsfromkeyvaluepair(temptable);
end; function lua_sortandtake(tableneedsort,takecount,isasc)
if isasc==true then
table.sort(tableneedsort);
else
table.sort(tableneedsort,function(a,b) return a>b end );
end;
local result={};
for i= ,takecount do
result[i]=tableneedsort[i];
end;
return result;
end;

lua里面求int数组的union,diff,inter,distinct 方法实现的更多相关文章

  1. c++ 求int数组的长度

    c++ 求int数组的长度 网上有一些方法是 sizeof(arr) / sizeof(arr[0]); 这种方法放在函数中,是不对的 我自己的方法是 #include <bits/stdc++ ...

  2. C#-求int数组中连续偶数列的个数

    例如:[3, 3, 2, 2, 2, 4, 3, 5, 4, 6, 3]=>2,2,2,4;4,6 结果为2     [3, 3, 2,3, 2, 2, 4, 3, 5, 4, 6, 3]=&g ...

  3. 求int型数组和最大子数组 续

    之前的博文里已经实现过该程序的构思.编译.运行,本次就不再重复与之相雷同的内容. 题目:与别人借组,借助求int型数组最大和子数组的问题,考虑大数溢出和int取值范围的问题 要求: 调试程序  当子数 ...

  4. 黑马基础阶段测试题:定义一个int类型的数组,数组中元素为{5,7,3,9,4}。求出数组中的最小值,并判断最小值是否为偶数,如果是偶数则输出“最小值为偶数”,如果不是偶数则输出“最小值为奇数”。打印如下:

    package com.swift; import java.util.Arrays; public class ArrayTest { public static void main(String[ ...

  5. 求一个数组中最小的K个数

    方法1:先对数组进行排序,然后遍历前K个数,此时时间复杂度为O(nlgn); 方法2:维护一个容量为K的最大堆(<算法导论>第6章),然后从第K+1个元素开始遍历,和堆中的最大元素比较,如 ...

  6. C# 之 将string数组转换到int数组并获取最大最小值

    1.string 数组转换到 int 数组 " }; int[] output = Array.ConvertAll<string, int>(input, delegate(s ...

  7. C语言中怎么求动态数组大小

    先来个简单的样例 int a[] = {1,2,3}; int arr_len = 0; arr_len = sizeof(a)/sizeof(int); 解释:sizeof() keyword是求出 ...

  8. Java后缀数组-求sa数组

    后缀数组的一些基本概念请自行百度,简单来说后缀数组就是一个字符串所有后缀大小排序后的一个集合,然后我们根据后缀数组的一些性质就可以实现各种需求. public class MySuffixArrayT ...

  9. C# int数组转string字符串

    方式一:通过循环数组拼接的方式: int[] types = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; string result = string.Empty ...

随机推荐

  1. C++设计模式-备忘录模式(1)

    备忘录模式:备忘录对象时一个用来存储另外一个对象内部状态的快照对象. 备忘录模式的用意是在不破坏封装的条件下,将一个对象的状态捉住并外部化,存储起来从而可以在将来合适的时候把这个对象还原到存储起来的状 ...

  2. 关于16年2月14日以后上传AppStore出现:Missing iOS Distribution signing identity for...的问题

    2016年2月14日以后打包上传AppStore会发现出现如下的问题: 导致问题的原因是:下边这个证书过期了 以下是苹果官方给出的回应: Thanks for bringing this to the ...

  3. Web项目中删错文件怎么办

    在开发过程中,有时会因为手误将文件错误删除,会造成很大的困惑,今天看到一个网友分享的一种可以恢复文件的方式特别好用,现在分享给大家. 1.首先在删除文件的路径下创建与原来文件名字相同的文件. 2.在文 ...

  4. XtraReport 添加空行的办法,很详细

    这两天为了做报表,研究了一下XtraReport .为了添加空行,想了很多办法.其中如果有分组时,网上给出的办法就会失败.现将经验公布一下,希望各位都能少走弯路. 1.加入自定义函数CreateCel ...

  5. Android SVN的配置

    关于svn有不错的图文博文,先po一个个人认为很不错的博文:http://ask.android-studio.org/?/article/97 接下来呢呢就是以我个人的思维模式来讲解svn啦: 当然 ...

  6. spring-data-redis注册fastjson序列化工具

    使用spring-data-redis的时候,其序列化工具自带:

  7. go的mgo,连接未释放问题,连接泄露。

    api启动几天后,卡住(连接失败,超时) 异常原因 mongo连接被占满,无法建立mgo连接,返回信息 查询点用端口可知,97%的连接被api项目占用. api项目的mongodb连接“泄露”,某处的 ...

  8. 蚁群算法求解旅行商问题(附c和matlab源代码)

    前几天写了个模拟退火算法的程序,然后又陆陆续续看了很多群智能算法,发现很多旅行商问题都采用蚁群算法来求解,于是开始写蚁群算法的模板.网上关于蚁群算法的理论很多就不再这里赘述了,下面直接上代码和进行简单 ...

  9. JAVA中获得一个月最大天数的方法(备忘)

    Calendar 类是一个抽象类,为日历字段之间的转换提供了一些方法.其中有一个重要方法 getActualMaximum ,该方法用于返回指定日历字段实际的最大值. 利用这个方法(Calendar. ...

  10. js将json字符串转化成json对象的方法

    js将json字符串转化成json对象的方法: JSON.parse(jsonObject)