leetcode77 组合
class Solution { public List<List<Integer>> combine(int n, int k) { List<List<Integer>> res = new ArrayList<>(); if(n<k||k<=0) return res; Deque<Integer> path = new LinkedList<Integer>(); dfs(res,path,1,n,k); return res; } private void dfs(List<List<Integer>> list, Deque<Integer> path, int i,int n, int k){ if(path.size()==k){ list.add(new ArrayList<Integer>(path)); return; } //利用剪枝的思想 for(int j=i;j<=n+1-(k-path.size());j++){ path.addLast(j); dfs(list,path,j+1,n,k); path.removeLast();//回溯 } }}
leetcode39 组合总和
//回溯+剪枝优化class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> res = new ArrayList<>(); Arrays.sort(candidates); if(candidates[0]>target) return res; Deque<Integer> path = new LinkedList<>(); int index = 0; dfs(res, path, candidates, index, target); return res; } private void dfs(List<List<Integer>> res, Deque<Integer> path, int[] candidates, int index, int target){ if(target==0){ res.add(new ArrayList<Integer>(path)); return; } for(int i=index;i<candidates.length;i++){ if(target-candidates[i]<0) break; path.addLast(candidates[i]); dfs(res,path,candidates,i,target-candidates[i]); path.removeLast(); } }}
leetcode40 组合总和II
class Solution { public List<List<Integer>> combinationSum2(int[] candidates, int target) { List<List<Integer>> res = new ArrayList<>(); Deque<Integer> path = new LinkedList<>(); Arrays.sort(candidates); dfs(candidates,res,path,0,target); return res; } private void dfs(int[] candidates,List<List<Integer>> res,Deque<Integer> path, int begin, int target){ if(target==0){ List<Integer> list = new ArrayList<>(path); res.add(list); return; } for(int i=begin;i<candidates.length;i++){ if(target-candidates[i]<0) break; //保证在本层不重复,在不同层可重复 if(i>begin&&candidates[i]==candidates[i-1]){ continue; } path.addLast(candidates[i]); dfs(candidates,res,path,i+1,target-candidates[i]); path.removeLast(); } }}
leetcode46 全排列
class Solution { public List<List<Integer>> permute(int[] nums) { int len = nums.length; boolean[] flag = new boolean[len]; List<List<Integer>> res = new ArrayList<>(); LinkedList<Integer> path = new LinkedList<>(); backtrack(res, path, flag, nums); return res; } public void backtrack(List<List<Integer>> res, LinkedList<Integer> path, boolean[] flag, int[] nums){ int len = nums.length; if(path.size()==len){ res.add(new ArrayList<>(path)); return; //剪枝 } for(int i=0;i<len;i++){ if(!flag[i]){ path.add(nums[i]); flag[i] = true; backtrack(res, path, flag, nums); path.removeLast(); flag[i] = false; } } }}
leetcode47 全排列 II
class Solution { public List<List<Integer>> permuteUnique(int[] nums) { int len = nums.length; boolean[] used = new boolean[len]; List<List<Integer>> res = new ArrayList<>(); Deque<Integer> path = new LinkedList<>(); Arrays.sort(nums); dfs(nums,res,path,used); return res; } private void dfs(int[] nums, List<List<Integer>> res, Deque<Integer> path,boolean[] used){ if(path.size()==nums.length) { res.add(new ArrayList<Integer>(path)); return; } for(int i=0;i<nums.length;i++){ if(i>0&&nums[i]==nums[i-1]&&!used[i-1]) continue;//去重 if(used[i]==false){ used[i] = true; path.addLast(nums[i]); dfs(nums,res,path,used); used[i] =false;//关键 path.removeLast(); } } }}
leetcode 22 括号生成
class Solution { public List<String> generateParenthesis(int n) { List<String> res = new ArrayList<>(); if(n==0) return res; dfs("",0,0,n,res); return res; } public void dfs(String path,int left,int right,int n,List<String> res){ if(left==n&&right==n){ res.add(path); return; } //如果左括号用的比右括号少,则返回 if(left<right){ return; } if(left<n){ dfs(path+"(",left+1,right,n,res); } if(right<n){ dfs(path+")",left,right+1,n,res); } }}
leetcode79 单词搜索
class Solution { public boolean exist(char[][] board, String word) { for(int i=0;i<board.length;i++){ for(int j=0;j<board[0].length;j++){ if(search(board, word, i, j, 0)){ return true; } } } return false; } public boolean search(char[][] board, String word, int i, int j, int k){ if(k>=word.length()){ return true; } if(i>=board.length||i<0||j<0||j>=board[0].length||board[i][j]!=word.charAt(k)){ return false; } board[i][j]+=100; boolean res = search(board, word, i+1, j, k+1)||search(board, word, i-1, j, k+1)||search(board, word, i, j+1, k+1)||search(board, word, i, j-1, k+1); board[i][j]-=100; return res; }}
leetcode 17 电话号码的字母组合
class Solution { // 存放电话按键对应的字母 String[] map = {"abc", "def", "ghi", "jkl", "mno", "qprs", "tuv", "wxyz"}; public List<String> res = new ArrayList<>(); public List<String> letterCombinations(String digits) { // 判断是否为空 if(digits.isEmpty()){ return res; } StringBuilder path = new StringBuilder(); backtrack(0,digits,path); return res; } public void backtrack(int i, String digits, StringBuilder path){ if(path.length()==digits.length()){ res.add(path.toString()); return; } String str = map[digits.charAt(i)-'2']; for(int m=0;m<str.length();m++){ path.append(str.charAt(m)); backtrack(i+1,digits,path); path.deleteCharAt(path.length()-1); } }}
leetcode 24 括号生成
class Solution { public List<String> res; public List<String> generateParenthesis(int n) { res = new ArrayList<>(); generate("", 0,0,n); return res; } public void generate(String str, int leftCount, int rightCount, int n){ if(leftCount>n||rightCount>n){ return; } if(leftCount==n&&rightCount==n){ res.add(str); } if(leftCount>=rightCount){ String newStr = new String(str); generate(newStr+"(",leftCount+1,rightCount,n); generate(newStr+")",leftCount,rightCount+1,n); } }}