LeetCode-90-Subsets II(回溯)-Medium
来源:互联网 发布:淘宝怎么买原味内衣 编辑:程序博客网 时间:2024/06/06 03:41
题意理解:
同题78,但所给数列中可能存在重复元素;
题目分析:
1. 解题思路同题78;
2. 在同级遍历中,如果发现当前元素在之前已出现,则跳过;
解题代码:
public class Solution { private ArrayList<ArrayList<Integer>> ans= new ArrayList<ArrayList<Integer>>(); private boolean find(int[] nums, int indexBegin, int indexCur, int n){ if(indexCur==indexBegin){ return false; } boolean ans=false; for(int i=indexBegin; i<indexCur; i++){ if(nums[i]==n){ ans=true; break; } } return ans; } private void process(ArrayList<Integer> list, int[] nums, int initPos, int n){ if(n==0){ ans.add(list); return; } if(initPos==nums.length){ return; } for(int i=initPos; i<nums.length; i++){ if(find(nums, initPos, i, nums[i])){ continue; } ArrayList<Integer> tList= new ArrayList(list); tList.add(new Integer(nums[i])); process(tList, nums, i+1, n-1); } } public List<List<Integer>> subsetsWithDup(int[] nums) { Arrays.sort(nums); for(int i=0; i<=nums.length; i++){ ArrayList<Integer> list=new ArrayList<Integer>(); process(list, nums, 0, i); } return (List)ans; }}
0 0
- LeetCode-90-Subsets II(回溯)-Medium
- LeetCode #90 - Subsets II - Medium
- [leetcode][回溯] Subsets II
- LeetCode-90-Subsets II 暴力dfs回溯
- LeetCode-78-Subsets(回溯法)-Medium
- [leetcode 78 & 90, Medium] Subsets I and II
- Leetcode 90. Subsets II (Medium) (cpp)
- 【leetcode题解】【回溯】【54】【M】Subsets II
- (回溯法)LeetCode#90. Subsets II
- 78.leetcode Subsets(medium)[回溯递归循环调用combination即可]
- [leetcode 90] Subsets II
- [LeetCode 90]Subsets II
- leetcode || 90、Subsets II
- Leetcode #90 Subsets II
- LeetCode(90) Subsets II
- leetcode 90: Subsets II
- LeetCode 90:Subsets II
- ***LeetCode 90 Subsets ii
- POJ 1852 Ants
- Python2.6和Python3.0中八进制数字表示区别
- WKWebView API精讲(OC)
- Linux常用命令
- 【Android】输入金额时自动补加一个元,金额删干净时,元字自动消失
- LeetCode-90-Subsets II(回溯)-Medium
- 匿名对象,构造函数,this关键字
- 4.Java IO-管道
- 如何在git.oschina.net部署项目
- httpServlet应用
- c#.net 生成清晰缩略图的解决方案
- OpenGL与OpenCV实现增强现实
- << React Native 入门与实战>>----第2章 React Native开发基础
- js BOM对象