46. 全排列

    xiaoxiao2025-04-15  29

    class Solution {     public List<List<Integer>> permute(int[] nums) {    if (nums == null || nums.length == 0)         return new ArrayList<List<Integer>>();     List<List<Integer>> result = new ArrayList<List<Integer>>();     Permutations(nums, 0, result);

        return result;     }          public static void Permutations(int[] a, int begin, List<List<Integer>> result) {     ArrayList<Integer> list = new ArrayList<Integer>();     int n = a.length, tmp;     if (begin == n) {         for (int i = 0; i < n; i++)             list.add(a[i]);         result.add(list);     } else {         for (int i = begin; i < n; i++) {             tmp = a[begin];             a[begin] = a[i];             a[i] = tmp;             Permutations(a, begin + 1, result);             tmp = a[begin];             a[begin] = a[i];             a[i] = tmp;         }     } } }

     

    最新回复(0)