全排列

作者: cuzz_ | 来源:发表于2018-06-13 11:57 被阅读0次
    • 递归的版本


      image.png
    public class Solution {
        /*
         * @param nums: A list of integers.
         * @return: A list of permutations.
         */
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        
        public List<List<Integer>> permute(int[] nums) {
            // write your code here
            List<Integer> list = new ArrayList<Integer>();
            if (nums == null || nums.length == 0) {
                res.add(list);
                return res;
            }
            permuteHelper(nums, 0);
            return res;
       }
       
       private void permuteHelper(int[] nums, int i){
           if (i == nums.length - 1) {
               list.add(nums[k]);
               res.add(list);
           } else {
               for (int j = i; j < nums.length; j++) {
                   exch(nums, i, j);
                   //System.out.println(i);
                   permuteHelper(nums, i + 1);
                   // 恢复原来的情况
                   exch(nums, i, j);
               }
           }
       }
       
       private void exch(int[] nums, int i, int j) {
           
           //System.out.println("i = " + i + "  j = " + j);
           int temp = nums[i];
           nums[i] = nums[j];
           nums[j] = temp;
       }
    }
    

    相关文章

      网友评论

          本文标题:全排列

          本文链接:https://www.haomeiwen.com/subject/vakjeftx.html