lua里面求int数组的union,diff,inter,distinct 方法实现
--利用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 方法实现的更多相关文章
- c++ 求int数组的长度
c++ 求int数组的长度 网上有一些方法是 sizeof(arr) / sizeof(arr[0]); 这种方法放在函数中,是不对的 我自己的方法是 #include <bits/stdc++ ...
- 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 ...
- 求int型数组和最大子数组 续
之前的博文里已经实现过该程序的构思.编译.运行,本次就不再重复与之相雷同的内容. 题目:与别人借组,借助求int型数组最大和子数组的问题,考虑大数溢出和int取值范围的问题 要求: 调试程序 当子数 ...
- 黑马基础阶段测试题:定义一个int类型的数组,数组中元素为{5,7,3,9,4}。求出数组中的最小值,并判断最小值是否为偶数,如果是偶数则输出“最小值为偶数”,如果不是偶数则输出“最小值为奇数”。打印如下:
package com.swift; import java.util.Arrays; public class ArrayTest { public static void main(String[ ...
- 求一个数组中最小的K个数
方法1:先对数组进行排序,然后遍历前K个数,此时时间复杂度为O(nlgn); 方法2:维护一个容量为K的最大堆(<算法导论>第6章),然后从第K+1个元素开始遍历,和堆中的最大元素比较,如 ...
- C# 之 将string数组转换到int数组并获取最大最小值
1.string 数组转换到 int 数组 " }; int[] output = Array.ConvertAll<string, int>(input, delegate(s ...
- C语言中怎么求动态数组大小
先来个简单的样例 int a[] = {1,2,3}; int arr_len = 0; arr_len = sizeof(a)/sizeof(int); 解释:sizeof() keyword是求出 ...
- Java后缀数组-求sa数组
后缀数组的一些基本概念请自行百度,简单来说后缀数组就是一个字符串所有后缀大小排序后的一个集合,然后我们根据后缀数组的一些性质就可以实现各种需求. public class MySuffixArrayT ...
- C# int数组转string字符串
方式一:通过循环数组拼接的方式: int[] types = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; string result = string.Empty ...
随机推荐
- DB2 runstats、reorgchk、reorg 命令
runstats.reorgchk.reorg 1.runstats runsats可以搜集表的信息,也可以搜集索引信息.作为runstats本身没有优化的功能,但是它更新了统计信息以后,可以让DB2 ...
- poj 1276 Cash Machine(多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33444 Accepted: 12106 De ...
- ubuntu14.04 安装系统
p { margin-bottom: 0.1in; line-height: 120% } code.cjk { font-family: "Droid Sans Fallback" ...
- PL/SQL远程连接Oracle的方式,无需安装客户端
第一次用PL/SQL Developer这个非官方的软件,遇到了种种困难,幸好通过万能的Google已经全部解决,记录下来备察. 前提是保证你的远程oracle服务器一切正常. 微软客户端设置 使 ...
- Android布局6大类
1:在我们Android开发中,常见的布局的方式有6大类 线性布局LinearLayout 相对布局RelativeLayout 表格布局TableLayout 单帧布局FrameLayout 绝对布 ...
- new(C# 参考)
在 C# 中,new 关键字可用作运算符.修饰符或约束. new 运算符 用于创建对象和调用构造函数. new 修饰符 用于隐藏基类中被继承的成员. new 约束 用于在泛型声明中约束可能用作类型参数 ...
- Spring bean的作用域和生命周期
bean的作用域 1.singleton,prototype, web环境下:request,session,gloab session 2.通过scope="" 来进行配置 3. ...
- sandy bridge
SANDY BRIDGE SPANS GENERATIONS Intel Focuses on Graphics, Multimedia in New Processor Design By Li ...
- TCP/IP详解系列 --- 概念总结01
UDP协议 .vs. TCP协议: 原理上:(TCP报文段. vs . UDP用户数据报) TCP协议的特性: TCP是面向连接的运输层协议,应用程序在使用TCP协议之前,必须先建立TCP连接. ...
- 有向图强连通分量的Tarjan算法
有向图强连通分量的Tarjan算法 [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G ...