def permute_wrapper(digits):
input = [i for i in range(1, digits+1)]
print(input)
output = [0 for x in range(len(input))]
flags = [False for x in range(len(input))]
depth = 0
for i in range(len(input)):
permute_(input, i+1, output, flags, depth, nums)
return nums
def permute_(input, limit, output, flags, depth, nums):
if depth == limit:
num = 0
factor = 1
for i in range(limit, 0, -1):
num = num + output[i - 1] * factor
factor *= 10
nums.append(num)
return None
for i in range(limit):
if flags[i] == True:
continue
flags[i] = True
output[depth] = input[i]
permute_(input, limit, output, flags, depth + 1, nums)
flags[i] = False
permute_(in): permute_(int, new buffer(), new bool[in.length], 0, 0) permute_(in, out, used, depth, limit): if depth > 0 && out.length == limit: print(out) if depth == in.length: return for i=0 < in.length: if used[i]: continue used[i] = true out.append(in[i]) permute_(in, out, used, depth+1, max(i+1, limit)) used[i] = false out.length--
For combinations of digits, instead of using a string buffer for output, an int array is used. index is used to keep track of where to insert the digit. The number is between index and 0 of output array.
def combine_wrapper(digits):
input = [i for i in range(1, digits+1)]
print(input)
output = [0 for x in range(len(input))]
start = 0
index = 0
combine_(input, output, start, index, nums)
return nums
def combine_(input, output, start, index, nums):
if start > 0:
num = 0
factor = 1
for x in range(index, -1, -1):
num = num + output[x] * factor
factor *= 10
nums.append(num)
if start == len(input):
return None
for i in range(start, len(input), 1):
output[index] = input[i]
combine_(input, output, i + 1, index + 1, nums)
No comments:
Post a Comment