java常见算法
欢迎关注 每日持续更新优质题目
-
回溯
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。
示例 1:
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。
示例 2:
输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
示例 3:
输入: candidates = [2], target = 1
输出: []
class Solution { ArrayList<List> lists = new ArrayList(); int len; ArrayList integers = new ArrayList(); public List<List> combinationSum(int[] candidates, int target) { len = candidates.length; backtrink(0,target,candidates,0); return lists; } public void backtrink(int sum,int target,int[] candidates,int indexStart) { if (sum > target || indexStart >= len) return; if (sum == target) { ArrayList arrayList = new ArrayList(this.integers); lists.add(arrayList); return; } for (int i = indexStart; i < len; i++) { integers.add(candidates[i]); backtrink(sum + candidates[i],target,candidates,i); integers.remove(integers.size() - 1); } }}
2.回溯-------去重 重要
给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用 一次 。
注意:解集不能包含重复的组合。
示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
示例 2:
输入: candidates = [2,5,2,1,2], target = 5,
输出:
[
[1,2,2],
[5]
]
class Solution { ArrayList<List> lists = new ArrayList(); int len; ArrayList integers = new ArrayList(); public List<List> combinationSum2(int[] candidates, int target) { len = candidates.length; Arrays.sort(candidates); backtrink(target,candidates,0); return lists; } public void backtrink(int target,int[] candidates,int indexStart) { if (target < 0) return; if (target == 0) { ArrayList arrayList = new ArrayList(this.integers); if (lists.contains(arrayList)) return; lists.add(arrayList); return; } for (int i = indexStart; i indexStart && candidates[i] == candidates[i-1]) continue; integers.add(candidates[i]); backtrink(target - candidates[i],candidates,i + 1); integers.remove(integers.size() - 1); } }}
3.
给出一个字符串数组 words 组成的一本英语词典。返回 words 中最长的一个单词,该单词是由 words 词典中其他单词逐步添加一个字母组成。
若其中有多个可行的答案,则返回答案中字典序最小的单词。若无答案,则返回空字符串。
示例 1:
输入:words = ["w","wo","wor","worl", "world"]
输出:"world"
解释:单词"world"可由"w", "wo", "wor", 和 "worl"逐步添加一个字母组成。
示例 2:
输入:words = ["a", "banana", "app", "appl", "ap", "apply", "apple"]
输出:"apple"
解释:"apply" 和 "apple" 都能由词典中的单词组成。但是 "apple" 的字典序小于 "apply"
一点一点匹配
class Solution { public String longestWord(String[] words) { Arrays.sort(words, (a, b) -> { if (a.length() != b.length()) { return a.length() - b.length(); } else { return b.compareTo(a); } }); HashSet set = new HashSet(); int len = words.length; String longest = ""; set.add(""); for (int i = 0; i < len; i++) { String word = words[i]; if (set.contains(word.substring(0,word.length()-1))) { set.add(word); longest = word;} } return longest; }}
4.深度优先
小朋友 A 在和 ta 的小伙伴们玩传信息游戏,游戏规则如下:
有 n 名玩家,所有玩家编号分别为 0 ~ n-1,其中小朋友 A 的编号为 0
每个玩家都有固定的若干个可传信息的其他玩家(也可能没有)。传信息的关系是单向的(比如 A 可以向 B 传信息,但 B 不能向 A 传信息)。
每轮信息必须需要传递给另一个人,且信息可重复经过同一个人
给定总玩家数 n,以及按 [玩家编号,对应可传递玩家编号] 关系组成的二维数组 relation。返回信息从小 A (编号 0 ) 经过 k 轮传递到编号为 n-1 的小伙伴处的方案数;若不能到达,返回 0。
示例 1:
输入:n = 5, relation = [[0,2],[2,1],[3,4],[2,3],[1,4],[2,0],[0,4]], k = 3
输出:3
解释:信息从小 A 编号 0 处开始,经 3 轮传递,到达编号 4。共有 3 种方案,分别是 0->2->0->4, 0->2->1->4, 0->2->3->4。
class Solution { int count,n,k; List<List> res; public int numWays(int n, int[][] relation, int k) { count = 0; this.k = k; this.n = n; res = new ArrayList<List>(); int len = relation.length; for(int i = 0;i < n;i++) { res.add(new ArrayList()); } for(int[] arr : relation) { int a = arr[0],b = arr[1]; res.get(a).add(b); } dfs(0,0); return count; } public void dfs(int index,int steps) { if(steps == k) { if(index == n - 1) count++; return; } List list = res.get(index); for(int next : list) { dfs(next,steps + 1); } }}