当前位置:网站首页>LeetCode brushing series -- 46. Full arrangement

LeetCode brushing series -- 46. Full arrangement

2022-08-11 07:28:00 Wooden water in the river island

Given an array nums without repeating numbers, return all possible permutations of it.You can return answers in any order.

Example 1:

Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Example 2:

Input: nums = [0,1]
Output: [[0,1],[1,0]]
Example 3:

Input: nums = [1]
Output: [[1]]

Tip:

1 <= nums.length <= 6
-10 <= nums[i] <= 10
All integers in nums are different from each other

Source: LeetCode
Link: https://leetcode.cn/problems/permutations

Thinking:

This question can be backtracked

java code:

class Solution {public List> permute(int[] nums) {List> result = new ArrayList<>();fullPermutation(nums, result, new LinkedList<>());return result;}public void fullPermutation(int[] nums, List> result, List subList) {// Indicates that all elements have been arranged in this arrangementif (subList.size() == nums.length) {result.add(new LinkedList<>(subList));return;}for (int num : nums) {if (subList.contains(num)) {continue;}subList.add(num);fullPermutation(nums, result, subList);subList.remove(subList.size() - 1);}}}

原网站

版权声明
本文为[Wooden water in the river island]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110552526104.html