我将从将您的数据从具有重复的列表转换为将 10 个可能数字中的每一个映射到可用次数的字典开始。有些条目的重复次数为 0,表示不使用该数字。
我们将从左到右进行,这样我们就可以处理在最左边的位置不使用 0 的业务。
1)对于最左边的位置,使用字典建立一个候选数字列表。将 0 排除在此列表之外。2) 建立下一个地方的候选名单,考虑之前的选择。每个候选列表都放在一个堆栈上。
对于 OP 给出的数据 0、1、2、2、4、4,我们构建以下堆栈:
{4} num = 102244
{4} num = 10224_
{2, 4} num = 1022__
{2, 4} num = 102___ (0 and 1 are all used)
{0, 2, 4} num = 10____ (since we've used all the ones)
{1, 2, 4} num = 1_____ (since 0 is not allowed on the left)
3) 回溯。从堆栈顶部的选择列表中删除第一个数字,它已被使用。将下一个选项放在整数的适当位置。如果没有更多选择,则弹出堆栈,直到可以做出选择为止。
所以在第 3 步之后,堆栈看起来像:
{4} num = 1024__
{2, 4} num = 102___ (0 and 1 are all used)
{0, 2, 4} num = 10____ (since we've used all the ones)
{1, 2, 4} num = 1_____ (since 0 is not allowed on the left)
4) 按照步骤 1 和 2 重建堆栈。
{4} num = 102424
{2, 4} num = 10242_
{4} num = 1024__
{2, 4} num = 102___ (0 and 1 are all used)
{0, 2, 4} num = 10____ (since we've used all the ones)
{1, 2, 4} num = 1_____ (since 0 is not allowed on the left)
5) 以这种方式继续下去,直到无法做出更多选择为止。
请注意,此方法仅发出有效数字,并且不会重复。
工作python代码:
data = [0, 1, 2, 2, 4, 4]
results = []
# Number of digits
n = len(data)
# initialize the counts
counts = dict((i, 0) for i in range(10))
for digit in data:
counts[digit] += 1
# We use this to keep track of the digits that have been used
used = dict((i, 0) for i in range(10))
choice_stack = []
# This is where we'll store the digits
slots = []
first = True
while first or len(choice_stack) > 0:
# build phase
while True:
choices = []
for i in range(10):
# no 0 allowed in the left-most place
if i == 0 and first:
first = False
continue
if counts[i] - used[i] > 0:
choices.append(i)
# Leave the build phase if we've used all of our digits
if len(choices) == 0:
break;
choice_stack.append(choices)
slots.append(choices[0])
used[choices[0]] += 1
# Compute the integer
num = 0
for i in range(n):
num += 10**i * slots[-i - 1]
results.append(num)
# backtrack phase
while len(choice_stack) > 0:
choices = choice_stack.pop()
slots.pop()
used[choices[0]] -= 1
del choices[0]
if len(choices) == 0:
continue
# next choice
choice_stack.append(choices)
slots.append(choices[0])
used[choices[0]] += 1
break
# Format the results
import sys
for i in range(len(results)):
if i%6:
sys.stdout.write(' ')
else:
sys.stdout.write('\n')
sys.stdout.write(str(results[i]))
sys.stdout.write('\n')
输出是:
102244 102424 102442 104224 104242 104422
120244 120424 120442 122044 122404 122440
124024 124042 124204 124240 124402 124420
140224 140242 140422 142024 142042 142204
142240 142402 142420 144022 144202 144220
201244 201424 201442 202144 202414 202441
204124 204142 204214 204241 204412 204421
210244 210424 210442 212044 212404 212440
214024 214042 214204 214240 214402 214420
220144 220414 220441 221044 221404 221440
224014 224041 224104 224140 224401 224410
240124 240142 240214 240241 240412 240421
241024 241042 241204 241240 241402 241420
242014 242041 242104 242140 242401 242410
244012 244021 244102 244120 244201 244210
401224 401242 401422 402124 402142 402214
402241 402412 402421 404122 404212 404221
410224 410242 410422 412024 412042 412204
412240 412402 412420 414022 414202 414220
420124 420142 420214 420241 420412 420421
421024 421042 421204 421240 421402 421420
422014 422041 422104 422140 422401 422410
424012 424021 424102 424120 424201 424210
440122 440212 440221 441022 441202 441220
442012 442021 442102 442120 442201 442210