• leetcode : permutation


    Given a collection of distinct numbers, return all possible permutations.

    For example,
    [1,2,3] have the following permutations:

    [
      [1,2,3],
      [1,3,2],
      [2,1,3],
      [2,3,1],
      [3,1,2],
      [3,2,1]
    ]

    思路: backtrack
    public class Solution {
        public List<List<Integer>> permute(int[] nums) {
            
            List<List<Integer>> result = new ArrayList<List<Integer>>();
            if(nums == null || nums.length == 0) {
                return result;
            }
            ArrayList<Integer> list = new ArrayList<Integer>();
            helper(result,list,nums);
            return result;
        }
        
        public void helper(List<List<Integer>> result, ArrayList<Integer> list, int[] nums) {
            
            if(list.size() == nums.length) {
                result.add(new ArrayList<Integer>(list));
            }
            
            for(int i = 0; i < nums.length; i++) {
                if(list.contains(nums[i])) {
                    continue;
                }
                list.add(nums[i]);
                helper(result,list,nums);
                list.remove(list.size() - 1);
            }
        } 
    }
    
  • 相关阅读:
    9. 远程分支与本地分支管理
    8. Git 远程协作
    7. Git stash命令
    6. Git版本处理
    5. Git 本地分支命令
    4. Git 日志命令
    JVM垃圾回收分析
    python常用模块
    ubuntu18配置jetty9
    logback spring配置
  • 原文地址:https://www.cnblogs.com/superzhaochao/p/6394075.html
Copyright © 2020-2023  润新知