网站流量不够怎么办,网店代运营公司有哪些,天河区门户网站,长沙做网站大概多少钱题目
给定一个不含重复数字的数组 nums #xff0c;返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
示例 1#xff1a;
输入#xff1a;nums [1,2,3] 输出#xff1a;[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] 示例 2#xff1a;
输入#xff1…题目
给定一个不含重复数字的数组 nums 返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
示例 1
输入nums [1,2,3] 输出[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] 示例 2
输入nums [0,1] 输出[[0,1],[1,0]] 示例 3
输入nums [1] 输出[[1]]
提示
1 nums.length 6 -10 nums[i] 10 nums 中的所有整数 互不相同 题解
class Solution {public ListListInteger permute(int[] nums) {ListListInteger res new ArrayListListInteger();ListInteger output new ArrayListInteger();for (int num : nums) {output.add(num);}int n nums.length;backtrack(n, output, res, 0);return res;}public void backtrack(int n, ListInteger output, ListListInteger res, int first) {// 所有数都填完了if (first n) {res.add(new ArrayListInteger(output));}for (int i first; i n; i) {// 动态维护数组Collections.swap(output, first, i);// 继续递归填下一个数backtrack(n, output, res, first 1);// 撤销操作Collections.swap(output, first, i);}}
}
来自力扣官方题解