751. Generate All Unique Permutations of an Array

MediumRecursionRecursion

Given an integer array nums, return a list of all DISTINCT permutations of nums. The output list must be sorted lexicographically by element sequence. If nums has duplicate elements, duplicate permutations must appear only once.

Input: An integer array nums.

Output: Return a list of lists: sorted lexicographically.

Examples

Example 1
Input: [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Explanation: Six permutations in lex order.
Example 2
Input: [1,1,2]
Output: [[1,1,2],[1,2,1],[2,1,1]]
Explanation: Three distinct permutations.
Example 3
Input: [0]
Output: [[0]]
Explanation: Single-element array.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →