题目:
解析:
这题设计递归函数,主要把看如何剪枝
代码:
javaclass Solution { private List<List<Integer>> ret; private List<Integer> path; private boolean[] check; public List<List<Integer>> permuteUnique(int[] nums) { ret = new ArrayList<>(); path = new ArrayList<>(); check = new boolean[nums.length]; Arrays.sort(nums);// dfs(nums,0); return ret; } //pos是决策树层数 private void dfs(int[] nums,int pos){ if(pos == nums.length){ ret.add(new ArrayList<>(path)); return; } for(int i = 0; i < nums.length; i++){ //不合法分支剪枝 if(check[i] == true || i != 0 && nums[i] == nums[i-1] && check[i-1] == false) continue; if(check[i] == false){ path.add(nums[i]); check[i] = true; dfs(nums,pos+1); //恢复现场 path.remove(path.size()-1); check[i] = false; } } } }
穷举vs暴搜vs深搜vs回溯vs剪枝专题一>全排列II
robin_suli2024-12-24 15:27
相关推荐
Suc_zhan35 分钟前
实验二 如何将随机森林算法应用于激酶抑制剂分类任务小学仔41 分钟前
leetcode -编辑距离AI是这个时代的魔法1 小时前
Using Dyck Path to solve a leetcode puzzle愚戏师1 小时前
数据结构与算法分析:树与哈希表(一)float_六七1 小时前
C++中的搜索算法实现电科_银尘2 小时前
【Matlab】-- 基于MATLAB的飞蛾扑火算法与反向传播算法的混凝土强度预测wen__xvn2 小时前
Codeforces Round 1014 (Div. 2)2092A - Kamilka and the Sheep c++梁下轻语的秋缘2 小时前
每日c/c++题 备战蓝桥杯(全排列问题)AredRabbit2 小时前
vector<int> 的用法pilgrim533 小时前
【二刷代码随想录】双指针-数组相关题型、推荐习题