diff --git a/src/DFS/CombinationSum2.java b/src/DFS/CombinationSum2.java new file mode 100644 index 0000000..b6844d4 --- /dev/null +++ b/src/DFS/CombinationSum2.java @@ -0,0 +1,45 @@ +package DFS; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class CombinationSum2 { + public List> combinationSum2(int[] candidates, int target) { + List> res = new ArrayList<>(); + Arrays.sort(candidates); + DFS(candidates, target, 0, new ArrayList<>(), res); + return res; + } + + private void DFS(int[] candidates, int target, int index, List currCombinations, List> res) { + + for(int i = index; i < candidates.length; ++ i) { + if(target < 0 || (i > index && candidates[i] == candidates[i-1])) { + // 当前target已经小于0,没必要再重复计算了 + // 重复值判断,必须i > index而不是0 + continue; + } + currCombinations.add(candidates[i]); + int nextTarget = target - candidates[i]; + if(nextTarget == 0) { + res.add(new ArrayList<>(currCombinations)); + } else { + // 取当前值 + DFS(candidates, nextTarget, i + 1, currCombinations, res); + } + + currCombinations.remove(currCombinations.size()-1); + // DFS(candidates, target, index + 1, currCombinations, res); + } + } + + public static void main(String[] args) { + CombinationSum2 combinationSum2 = new CombinationSum2(); + + int[] input1 = new int[]{10,1,2,7,6,1,5}; + + List> res1 = combinationSum2.combinationSum2(input1, 8); + System.out.println(res1.toString()); + } +} diff --git a/src/LongestConsecutive.java b/src/LongestConsecutive.java index 82db01b..eee68e1 100644 --- a/src/LongestConsecutive.java +++ b/src/LongestConsecutive.java @@ -1,4 +1,6 @@ +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; public class LongestConsecutive { @@ -51,5 +53,11 @@ public static void main(String[] a) { int[] input4 = new int[]{9, 1, 4, 7, 3, -1, 0, 5, 8, -1, 6}; System.out.println("res4:" + l.longestConsecutive(input4)); + List bb = new ArrayList<>(); + bb.add("aa"); + bb.add("bb"); + List aa = new ArrayList<>(bb); + + System.out.println(aa.toString()); } } diff --git a/src/Stack/AsteroidCollision.java b/src/Stack/AsteroidCollision.java new file mode 100644 index 0000000..b6db410 --- /dev/null +++ b/src/Stack/AsteroidCollision.java @@ -0,0 +1,57 @@ +package Stack; + +import java.util.Arrays; +import java.util.Deque; +import java.util.LinkedList; + +class AsteroidCollision { + public int[] asteroidCollision(int[] asteroids) { + Deque deque = new LinkedList<>(); + for (int i = 0; i < asteroids.length; ++i) { + int currAsteroid = asteroids[i]; + if (currAsteroid > 0) { + // 向右的行星,不可能和之前的发生碰撞,直接入栈 + deque.addLast(currAsteroid); + } else { + // 这里的逻辑可以优化为,循环里判断如果栈顶是正行星并且绝对值小于负行星,则直接弹出; + // 接着判断当前的负行星,如果栈为空,则直接入栈;如果栈顶正行星绝对值和负行星相等,则正行星出栈;如果大于,则什么都不做; + // 这样可以避免bool变量和break,不过逻辑有点晦涩 + boolean isCurrExploded = false; + while (!deque.isEmpty() && deque.peekLast() > 0) { + // 如果存活在栈顶的是负行星,则不需要考虑,直接入栈 + int prevAsteroid = deque.peekLast(); + int sum = prevAsteroid + currAsteroid; + if (sum == 0) { + // 栈顶正行星和当前负行星爆炸,退出循环 + deque.pollLast(); + isCurrExploded = true; + break; + } else if (sum < 0) { + // 栈顶的正行星爆炸 + deque.pollLast(); + } else { + // 当前负行星爆炸,退出循环 + isCurrExploded = true; + break; + } + } + if (!isCurrExploded) { + deque.addLast(currAsteroid); + } + } + } + return deque.stream().mapToInt(Integer::intValue).toArray(); + } + + public static void main(String[] args) { + int[] input1 = new int[]{1, 5, -10}; + AsteroidCollision asteroidCollision = new AsteroidCollision(); + int[] res = asteroidCollision.asteroidCollision(input1); + System.out.println(Arrays.toString(res)); + + int[] input2 = new int[]{-2, -2, 1, -2}; + int[] res2 = asteroidCollision.asteroidCollision(input2); + System.out.println(Arrays.toString(res2)); + + } +} diff --git a/src/dp/JumpGame.java b/src/dp/JumpGame.java new file mode 100644 index 0000000..ba4cc92 --- /dev/null +++ b/src/dp/JumpGame.java @@ -0,0 +1,22 @@ +package dp; + +import java.util.Arrays; + +public class JumpGame { + + // to be optimized + public int jump2(int[] nums) { + if(nums == null) {return 0;} + int n = nums.length; + int[] dp = new int[n]; + Arrays.fill(dp, Integer.MAX_VALUE); + dp[0] = 0; + for(int i = 1; i < n; ++ i) { + + for(int j = 1; j <= nums[i] && j < n - i; ++ j) { + dp[i + j] = Math.min(dp[i + j], dp[i] + 1); + } + } + return dp[n-1]; + } +}