深圳福田网站建设公司,沈阳百度seo排名优化软件,如何申请免费域名做网站,中国遵义网题目详情#xff1a;
给定一个不含重复数字的数组 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;
…题目详情
给定一个不含重复数字的数组 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]]代码实现
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);}}
}