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 ...
随机推荐
- [转载] Android随笔之——PackageManager详解
本文转载自: http://www.cnblogs.com/travellife/p/3932823.html 参考:http://www.cnblogs.com/xingfuzzhd/p/33745 ...
- Java垃圾回收机制 入门
对于Java虚拟机的了解,我认为是一个Java程序员已经入门的重要标志,而JVM中的垃圾回收机制(Garbage Collection,简称GC)又是JVM中的重点,所以hans在这里用篇文章时间和大 ...
- 使用date类和format类对系统当前时间进行格式化显示
一:Date------------String 代码1:(代码二对显示出来的时间格式进行优化) package DateDemo; import java.text.SimpleDateFormat ...
- 直接用<img> 的src属性显示base64转码后的字符串成图片
直接用<img> 的src属性显示base64转码后的字符串成图片 <img src="base64转码后的字符串" ></img> 下面的图片 ...
- transitionend的运用案例
transitionend事件代表着过渡动画结束后 原生的绑定方法 obj.addEventListener('transitionEnd', function(){ //do soming }) 我 ...
- linux 程序管理与SElinux
此文涉及的命令:&.jobs.fg.bg.kill.nohup.ps.top.pstree.free.uname.uptime.netstat.dmesg.vmstat.fuser.lsof. ...
- mysql从零开始
常用的数据库有哪些? oralce,sqlserver,mysql,db2 有钱就用oracle吧 oracle和mysql的区别:https://zhidao.baidu.com/question/ ...
- VBA Excel 对比两列数据
Sub Md() ' ' Macro1 Macro ' 宏由 BX 录制,时间: 2012-6-8 ' 宏中的列数可以输入 A - IV 也可以输入 1-256 ' Dim i%, j%, i1%, ...
- CSS基础选择器
1.html负责结构,css负责样式,js负责行为. css是写在head标签里面,容器style标签里面, <style type="text/css"> body{ ...
- jQuery Mobile 列表内容
jQuery Mobile 列表缩略图 对于大于 16x16px 的图像,请在链接中添加 <img> 元素. jQuery Mobile 将自动把图像调整至 80x80px: 实例: &l ...