1. class Solution {
    2. public List<List<Integer>> permute(int[] nums) {
    3. used = new boolean[nums.length];
    4. recur(0,nums);
    5. return ans;
    6. }
    7. boolean[] used;
    8. List<Integer> chosen = new ArrayList<>();
    9. int pos;
    10. List<List<Integer>> ans = new ArrayList<>();
    11. public void recur(int pos,int[] nums){
    12. //1:循环结束条件,当位置 = 数组长度的时候,保存当前已经选择的数组
    13. if(pos == nums.length){
    14. ans.add(new ArrayList<>(chosen));
    15. return;
    16. }
    17. // 2: 递归方法,也就是相似的方法
    18. for(int i = 0;i < nums.length;i++){
    19. if(!used[i]){
    20. chosen.add(nums[i]);
    21. used[i] = true;
    22. recur(pos + 1,nums);
    23. //3:还原现场
    24. chosen.remove(chosen.size()-1);
    25. used[i] = false;
    26. }
    27. }
    28. }
    29. }