Generate All Permutations of a Number
Updated
Problem statement
Given an integer array nums that holds the digits of a number, return every distinct permutation of those digits in lexicographic order.
A permutation uses every element of nums exactly once. The same digit can appear more than once in nums, and permutations that read the same are returned only once. A permutation may start with 0.
Lexicographic order compares two permutations from left to right: at the first position where they differ, the one with the smaller digit comes first.
Function
permuteUnique(nums: int[]) → List<List<Integer>>Examples
Example 1
nums = [2,1,2]return = [[1,2,2],[2,1,2],[2,2,1]]The two 2s are the same digit, so swapping them gives nothing new. Of the 6 ways to order three items, only 3 are different, and they are listed from smallest to largest.
Example 2
nums = [3,0,1]return = [[0,1,3],[0,3,1],[1,0,3],[1,3,0],[3,0,1],[3,1,0]]All three digits are different, so all 3 * 2 * 1 = 6 orderings appear. The ones that start with 0, such as [0,1,3], count too.
Example 3
nums = [9,9]return = [[9,9]]Both orderings of the two 9s read [9,9], so it is returned once.
Constraints
1 <= nums.length <= 80 <= nums[i] <= 9
More Fleetx Onsite Interview questions
Similar questions at other companies
- Bounded Combination Sum with Signed CandidatesConfluent · ONSITE INTERVIEWConfluent interview questions
- Enumerate Character Paths to a MarkerTesla · ONSITE INTERVIEWTesla interview questions
- Subsets IIBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Generate Circular Student ArrangementsAmazon · ONSITE INTERVIEW
- Enumerate Compatible Meeting SubsetsExpedia · ONSITE INTERVIEWExpedia interview questions
- Partition into Equal-Sum GroupsPalo Alto Networks · ONSITE INTERVIEWPalo Alto Networks interview questions
- PermutationsEPAM · PHONE SCREENEPAM interview questions
- Combination Sum with Single-Use ValuesPinterest · PHONE SCREENPinterest interview questions
- Combination Sum with Unlimited ReuseNVIDIA · PHONE SCREENNVIDIA interview questions
- Sort by Checking PermutationsMercor · PHONE SCREENMercor interview questions