Skip to content

Instantly share code, notes, and snippets.

@rangercyh
Last active December 18, 2015 17:39
Show Gist options
  • Save rangercyh/5820015 to your computer and use it in GitHub Desktop.
Save rangercyh/5820015 to your computer and use it in GitHub Desktop.
a lexicographical full array algorithm
--[[字典递增生成全排列,除去打印处理,只能说是效率低下,虽然核心程序只有12行,比较简洁]]
g_tbtest = {1,2,3}
function printTable(...)
local str = ""
local tbArgs = {...}
for i, tbStr in ipairs(tbArgs) do
if type(tbStr) == "table" then
for k, v in ipairs(tbStr) do
str = str..v
end
end
end
print(str)
end
function FullArray(tbtest, hh)
if #tbtest == 1 then
printTable(hh, tbtest)
else
for key, value in ipairs(tbtest) do
table.insert(hh, value)
table.remove(tbtest, key)
FullArray(tbtest, hh)
table.insert(tbtest, key, value)
table.remove(hh)
end
end
end
FullArray(g_tbtest, {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment