博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
子集和排列
阅读量:5157 次
发布时间:2019-06-13

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

1 Subsets

public List
> subsets(int[] nums) { List
> result = new ArrayList<>(); if (nums.length == 0) { return result; } ArrayList
list = new ArrayList<>(); subsetsHelper(nums, 0, list, result); return result; } void subsetsHelper(int[] nums, int pos, List
list, List
> result){ result.add(new ArrayList<>(list)); for (int i = pos; i < nums.length; i++) { list.add(nums[i]); subsetsHelper(nums, i + 1, list, result); list.remove(list.size() - 1); } }
View Code

 2 Subsets II

public List
> subsetsWithDup(int[] nums) { List
> result = new ArrayList<>(); if (nums.length == 0) { return result; } Arrays.sort(nums); ArrayList
list = new ArrayList<>(); subsetsHelper(nums, 0, list, result); return result; } void subsetsHelper(int[] nums, int pos, List
list, List
> result){ result.add(new ArrayList<>(list)); for (int i = pos; i < nums.length; i++) { if (i > pos && nums[i] == nums[i - 1]){ continue; } list.add(nums[i]); subsetsHelper(nums, i + 1, list, result); list.remove(list.size() - 1); } }
View Code

3 Permutations

public List
> permute(int[] nums) { // write your code here List
> res = new ArrayList<>(); if (nums == null || nums.length == 0) { res.add(new ArrayList
()); return res; } List
list = new ArrayList<>(); helper(nums, list, res); return res; } void helper(int[] nums, List
list, List
> res) { if (list.size() == nums.length) { res.add(new ArrayList<>(list)); return; } for (int i = 0; i < nums.length; i++) { if (list.contains(nums[i])) { continue; } list.add(nums[i]); helper(nums, list, res); list.remove(list.size() - 1); } }
View Code

4 Permutations II

public List
> permuteUnique(int[] nums) { // Write your code here List
> result = new ArrayList<>(); if (nums == null || nums.length == 0) { result.add(new ArrayList
()); return result; } Arrays.sort(nums); List
list = new ArrayList<>(); boolean[] used = new boolean[nums.length]; helper(nums, list, used, result); return result; } void helper(int[] nums, List
list, boolean[] used, List
> result) { if (list.size() == nums.length) { result.add(new ArrayList<>(list)); return; } for (int i = 0; i < nums.length; i++) { if (used[i] || (i > 0 && nums[i] == nums[i - 1] && !used[i - 1])) { continue; } used[i] = true; list.add(nums[i]); helper(nums, list, used, result); used[i] = false; list.remove(list.size() - 1); } }
View Code

5 Combination Sum    可以有重复

public ArrayList
> combinationSum(int[] candidates, int target) { ArrayList
> res = new ArrayList
>(); if (candidates == null || candidates.length == 0) { return res; } Arrays.sort(candidates); helper(candidates, 0, target, new ArrayList
(), res); return res; } private void helper(int[] candidates, int start, int target, ArrayList
item, ArrayList
> res) { // TODO Auto-generated method stub if (target < 0) { return; } if (target == 0) { res.add(new ArrayList<>(item)); return; } for (int i = start; i < candidates.length; i++) { if (i > 0 && candidates[i] == candidates[i - 1]) { continue; } item.add(candidates[i]); helper(candidates, i, target - candidates[i], item, res); item.remove(item.size() - 1); } }
View Cod

6 Combination Sum     不可以有重复

public List
> combinationSum2(int[] nums, int target) { List
> list = new ArrayList<>(); Arrays.sort(nums); backtrack(list, new ArrayList<>(), nums, target, 0); return list; }private void backtrack(List
> list, List
tempList, int [] nums, int remain, int start){ if(remain < 0) return; else if(remain == 0) list.add(new ArrayList<>(tempList)); else{ for(int i = start; i < nums.length; i++){ if(i > start && nums[i] == nums[i-1]) continue; // skip duplicates tempList.add(nums[i]); backtrack(list, tempList, nums, remain - nums[i], i + 1); tempList.remove(tempList.size() - 1); } }}
View Code

7 Palindrome Partitioning

public class Solution {    public List
> partition(String s) { List
> res = new ArrayList<>(); if (s.length() == 0) return res; dfs(s, 0, new ArrayList
(), res); return res; } public void dfs(String s, int index, ArrayList
item, List
> res){ if (s.length() == index) { res.add(new ArrayList<>(item)); return; } for (int i = index; i < s.length(); i++){ if (isP(s, index, i)){ item.add(s.substring(index, i + 1)); dfs(s, i + 1, item, res); item.remove(item.size() - 1); } } } public boolean isP(String s, int l, int r){ while (l < r) { if (s.charAt(l) != s.charAt(r)){ return false; } l++;r--; } return true; }}
View Code

 

转载于:https://www.cnblogs.com/whesuanfa/p/7435894.html

你可能感兴趣的文章
不同程序语言的注释和变量要求
查看>>
语言基础(9):static, extern 和 inline
查看>>
windows linux—unix 跨平台通信集成控制系统
查看>>
【编程练习】复习一下树的遍历
查看>>
邮件和短信验证码
查看>>
(转)Android studio 使用心得(五)—代码混淆和破解apk
查看>>
构建之法阅读笔记03
查看>>
ES5_03_Object扩展
查看>>
Apache-ab 接口性能测试
查看>>
EF 4.1 Code First Walkthrough
查看>>
常用MySQL语法
查看>>
007API网关服务Zuul
查看>>
bzoj 2600: [Ioi2011]ricehub
查看>>
iOS __strong __weak @Strongify @Weakify
查看>>
thinkphp引入PHPExcel类---thinkPHP类库扩展-----引入没有采用命名空间的类库
查看>>
创建数据库,表
查看>>
Luogu 1970 NOIP2013 花匠 (贪心)
查看>>
javascript笔记---貌似大叔
查看>>
去重查询表mysql 中数据
查看>>
工厂模式
查看>>