
这似乎有效,但有更有效的方法吗?
function array_sub(t1,t2)-- Substract Arrays from Array -- Usage: nretable = array_sub(T1,T2) -- removes T1 from T2 table.sort( t1 )for i = 1,#t2 do if (t2[i] ~= nil) then for j = 1,#t1 do if (t2[i] == t1 [j]) then table.remove (t2,i) end end endend return t2endlocal remove ={1,2,3} local full = {}; for i = 1,10 do full[i] = i endlocal test ={}local test = array_sub(remove,full)for i = 1,#test do print (test[i])end解决方法 是的,有:创建一个包含表t1的所有值的查找表,然后从结尾开始经过表t2. function array_sub(t1,t2) local t = {} for i = 1,#t1 do t[t1[i]] = true; end for i = #t2,1,-1 do if t[t2[i]] then table.remove(t2,i); end endend 交易O(#t1)空间,从O(#t1 * #t2)到O(#t1#t2)加速.
总结以上是内存溢出为你收集整理的从Lua中的表中减去表全部内容,希望文章能够帮你解决从Lua中的表中减去表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)