--利用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++程序

    #include <iostream> #include<windows.h> #include <shellapi.h> #include <stdio.h ...

  2. install LLVM

    version >= 3.8.0 $ cd llvm... $ mv someofClang ./tools $ mkdir build $ cd build $ cmake -DCMAKE_B ...

  3. angular+ionic返回上一页并刷新

    假定当前页面为editCata页面,要返回的是cataDetail页面.目前我找到两种方法实现返回上一页并刷新,如果以后有其它方法,再继续添加. 1.在editCataCtrl.js中注入$ionic ...

  4. 2016CCPC 合肥--最大公约数//每一年通向它的路上,多少人折戟沉沙,多少人功败垂成,有人一战成名,有人从头再来。

    有这样一个有关最大公约数的函数:函数 f(x, y): { c=0 当 y>0: { c +=1 t = x % y x = y y = t } 返回 c * x * x} 给出三个正整数n,m ...

  5. Centos 压缩、解压和打包命令

    gzip命令 --功能说明:gz文件的压缩和解压缩. --命令格式:gzip [参数] <文件> --常用参数: -d 解开压缩文件 -l 列出压缩文件的相关信息 -q 不显示警告信息 - ...

  6. JS浮点数的加减乘除运算

    文章来源地址:http://blog.csdn.net/lyd518/article/details/7236464 转载请注明出处,尊重作者劳动成果,谢谢!问题这样的: 37.5*5.5=206.0 ...

  7. Monkeyrunner小脚本关于camera的使用

    一下代码涉及自动执行camera,属性一个小设置,恢复初始值,并在中间添加截屏功能 将两个截屏进行前后对比,并返回值 适合初学者,刚刚了解monkeyrunner 的人员来看 注意:一下脚本如果不能执 ...

  8. 遇到IIS7配置PHP出现403和404错误的解决办法

    服务器要配置PHP,总是出现403错误.服务器是新装的,操作系统是windows server 2008 R2,装的IIS7. IIS里PHP和本地服务器对比了好几遍,都没到出错的原因,后来通过cmd ...

  9. 数据导出Excel

    package com.jy.pjbj.web.action.util; import java.io.File;import java.util.List; import javax.xml.rpc ...

  10. U盘启动盘 安装双系统 详细教程

    U盘启动盘 安装win7+linux双系统 最近在看鸟哥的linux 私房菜 ,看到多重系统那部分,自然的安装多重系统的激情由此而燃.在网上看了很多资料,感觉都不全.经过艰辛的摸索,终于被我发现了一个 ...