博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Backtracking(一)
阅读量:4494 次
发布时间:2019-06-08

本文共 2097 字,大约阅读时间需要 6 分钟。

LeetCode中涉及到回溯的题目有通用的解题套路:

 46. permutations

这一类回溯题目中的基础中的基础,无重复数字枚举:

1 /* Given a collection of distinct numbers, return all possible permutations. 2  3 For example, 4 [1,2,3] have the following permutations: 5 [ 6   [1,2,3], 7   [1,3,2], 8   [2,1,3], 9   [2,3,1],10   [3,1,2],11   [3,2,1]12 ] */13 14 public class Solution {15     public List
> permute(int[] nums) {16 List
> res = new ArrayList<>();17 backtrack(res, new ArrayList<>(), nums);18 return res;19 }20 public void backtrack(List
> list, List
tmp, int[] nums){21 if(tmp.size() == nums.length)22 list.add(new ArrayList<>(tmp)); //要重新new一个list进去23 else{24 for(int i = 0; i < nums.length; i++){25 if(tmp.contains(nums[i])) continue;26 tmp.add(nums[i]);27 backtrack(list, tmp, nums);28 tmp.remove(tmp.size() - 1); //回溯的重要一步,恢复环境29 30 }31 }32 }33 }

47. permutations II 

稍微增加了点难度,有重复的数字,采取的技巧其实是相当于对重复的数字人为规定一个顺序

1 /* Given a collection of numbers that might contain duplicates, return all possible unique permutations. 2  3 For example, 4 [1,1,2] have the following unique permutations: 5 [ 6   [1,1,2], 7   [1,2,1], 8   [2,1,1] 9 ] */10 11 public class Solution {12     public List
> permuteUnique(int[] nums) {13 List
> res = new ArrayList<>();14 if(nums == null || nums.length == 0) return res;15 Arrays.sort(nums); //排序不要忘16 backtrack(res, new ArrayList<>(), nums, new boolean[nums.length]);17 return res;18 }19 public void backtrack(List
> list, List
tmp, int[] nums, boolean[] used){20 if(tmp.size() == nums.length)21 list.add(new ArrayList<>(tmp));22 else{23 for(int i = 0; i < nums.length; i++){24 if(used[i] || (i > 0 && nums[i-1] == nums[i] && !used[i-1])) continue;25 used[i] = true;26 tmp.add(nums[i]);27 backtrack(list, tmp, nums, used);28 used[i] = false;29 tmp.remove(tmp.size() - 1);30 }31 }32 }33 }

 

转载于:https://www.cnblogs.com/niuxichuan/p/7352722.html

你可能感兴趣的文章
EditPlus VC2010 and 2008 C/C++配置
查看>>
Practical Lessons from Predicting Clicks on Ads at Facebook
查看>>
JFrame面板
查看>>
Android自动化压力测试之Monkey Test 异常解读(五)
查看>>
Compressing Convolutional Neural Networks in the Frequency Domain 论文笔记
查看>>
设计模式:单例和多例
查看>>
Myslq 之修改数据库
查看>>
maven工程转为web工程时没有add web project capabilities选项的解决办法
查看>>
[BZOJ1192][HNOI2006]鬼谷子的钱袋
查看>>
Linux系统基础优化
查看>>
小程序开发快速入门教程(附源码)
查看>>
基于cropper.js的图片上传和裁剪
查看>>
车联网SaaS平台多租户平台技术选型参考
查看>>
我是如何快速积累工作经验
查看>>
用信号量进程同步与互斥
查看>>
随笔1
查看>>
Codeforces Round #469 (Div. 2)
查看>>
JavaScript:Number 对象
查看>>
事务同步多线程
查看>>
怎么去掉联系人、通话记录、拨号列表界面中的电话号码中间的空格?
查看>>