用JavaScript实现1-9无重复任意位数全排列数组(递归适配)
Recursive Solution for Variable-Length Unique Digit Permutations
Hey there! I totally get why recursion is the way to go here—it makes adapting to different digit lengths a total breeze. Let's break down how to build a flexible recursive function that can generate 4-digit, 5-digit, or any-length permutations from your base array [1,2,3,4,5,6,7,8,9] with barely any code changes.
Core Recursive Idea
The logic is straightforward and scalable:
- Start with an empty current permutation and the full list of available digits.
- For each digit in the available list:
- Add it to the current permutation.
- Recurse with the updated permutation and the remaining unused digits.
- When the current permutation hits your target length, add it to the final result set.
This approach naturally adapts to any length you need—you only ever have to tweak one parameter to switch between 4-digit, 5-digit, or even 9-digit permutations.
Python Implementation
Here's a clean, adaptable code example that does exactly what you need:
def generate_permutations(base_digits, target_length): result = [] def backtrack(current_perm, remaining_digits): # Base case: if we've built a permutation of the target length, save it if len(current_perm) == target_length: # Convert the digit list to an integer (or keep as a list if you prefer) result.append(int(''.join(map(str, current_perm)))) return # Recursive case: iterate through every remaining unused digit for i in range(len(remaining_digits)): # Pick the i-th digit and add it to our current permutation selected_digit = remaining_digits[i] # Create a new list of remaining digits (excluding the one we just picked) updated_remaining = remaining_digits[:i] + remaining_digits[i+1:] # Recurse with the updated permutation and remaining digits backtrack(current_perm + [selected_digit], updated_remaining) # Kick off the recursion with an empty permutation and full base digits backtrack([], base_digits) return result # Example 1: Generate all 4-digit unique permutations base_array = [1,2,3,4,5,6,7,8,9] four_digit_perms = generate_permutations(base_array, 4) print(f"Generated {len(four_digit_perms)} unique 4-digit permutations") # Example 2: Want 5-digit permutations? Just change the target length! five_digit_perms = generate_permutations(base_array, 5) print(f"Generated {len(five_digit_perms)} unique 5-digit permutations")
How It Adapts to Different Lengths
See how simple it is to switch between digit lengths?
- For 4-digit permutations: call
generate_permutations(base_array, 4) - For 6-digit permutations: call
generate_permutations(base_array, 6)
No other code changes required—recursion handles the heavy lifting by building permutations one digit at a time until it hits your target length.
Quick Tweaks You Can Make
- If you prefer permutations as lists of digits instead of integers, replace
int(''.join(map(str, current_perm)))withcurrent_perm.copy(). - The maximum target length you can use is 9 (since your base array has 9 unique digits)—the function will just return all full permutations of the array when you pass
9as the target.
内容的提问来源于stack exchange,提问作者matthew gabriel
相关产品推荐
相关产品推荐

