From e918f47cca7dfe7540522ddc70763e54aefb3f80 Mon Sep 17 00:00:00 2001 From: whai Date: Sun, 25 Feb 2024 22:34:24 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=AA=E5=BF=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../whaifree/leetCode/Greedy/LeetCode322.java | 44 +++++++++++ .../whaifree/leetCode/Greedy/LeetCode376.java | 45 +++++++++++ .../whaifree/leetCode/Greedy/LeetCode455.java | 59 ++++++++++++++ .../whaifree/leetCode/Greedy/LeetCode53.java | 48 ++++++++++++ .../cn/whaifree/leetCode/LeetCode001.java | 70 ----------------- src/main/java/cn/whaifree/leetCode/Test.java | 76 +++++++++++++++++++ .../cn/whaifree/leetCode/Tree/LeetCode94.java | 3 + 7 files changed, 275 insertions(+), 70 deletions(-) create mode 100644 src/main/java/cn/whaifree/leetCode/Greedy/LeetCode322.java create mode 100644 src/main/java/cn/whaifree/leetCode/Greedy/LeetCode376.java create mode 100644 src/main/java/cn/whaifree/leetCode/Greedy/LeetCode455.java create mode 100644 src/main/java/cn/whaifree/leetCode/Greedy/LeetCode53.java delete mode 100644 src/main/java/cn/whaifree/leetCode/LeetCode001.java create mode 100644 src/main/java/cn/whaifree/leetCode/Test.java diff --git a/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode322.java b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode322.java new file mode 100644 index 0000000..fac8054 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode322.java @@ -0,0 +1,44 @@ +package cn.whaifree.leetCode.Greedy; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/25 22:21 + * @注释 + */ +public class LeetCode322 { + + + @Test + public void test() { + System.out.println(new Solution().maxProfit(new int[]{7, 1, 5, 3, 6, 4})); + } + + class Solution { + + + /** + * 随时可以买卖,那么只要有跌的我都不要。 + * 上帝视角:只要涨我就全要 + * + * 7跌 1涨5 跌 4涨6 跌4 + * + * @param prices + * @return + */ + public int maxProfit(int[] prices) { + + int maxProfit = 0; + for (int i = 0; i < prices.length; i++) { + if (i > 0 && prices[i] - prices[i - 1] > 0) { + maxProfit += prices[i] - prices[i - 1]; + } + } + return maxProfit; + } + + // TODO 动态规划 + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode376.java b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode376.java new file mode 100644 index 0000000..629585f --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode376.java @@ -0,0 +1,45 @@ +package cn.whaifree.leetCode.Greedy; + +import jdk.internal.instrumentation.InstrumentationTarget; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/24 13:23 + * @注释 + */ +public class LeetCode376 { + + @Test + public void test() { +// System.out.println(new Solution().wiggleMaxLength(new int[]{1, 7, 4, 9, 2, 5})); + System.out.println("========"); + System.out.println(new Solution().wiggleMaxLength(new int[]{1,17,5,10,13,15,10,5,16,8})); + } + + + class Solution { + public int wiggleMaxLength(int[] nums) { + if (nums.length <= 1) { + return nums.length; + } + //当前差值 + int curDiff = 0; + //上一个差值 + int preDiff = 0; + int count = 1; + for (int i = 1; i < nums.length; i++) { + //得到当前差值 + curDiff = nums[i] - nums[i - 1]; + //如果当前差值和上一个差值为一正一负 + //等于0的情况表示初始时的preDiff + if ((curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0)) { + count++; + preDiff = curDiff; + } + } + return count; + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode455.java b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode455.java new file mode 100644 index 0000000..ea45620 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode455.java @@ -0,0 +1,59 @@ +package cn.whaifree.leetCode.Greedy; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/24 13:04 + * @注释 + */ +public class LeetCode455 { + + @Test + public void test() { + System.out.println(new Solution().findContentChildren(new int[]{1, 2, 3,1}, new int[]{2,3,1})); + } + + class Solution { + /** + * + * @param g 孩子的胃口 + * @param s 饼干 + * @return + */ + public int findContentChildren(int[] g, int[] s) { + // 找到g中最大的,分配s中最大的看看是否满足 + Arrays.sort(g); + Arrays.sort(s); + + int res = 0; + + + int gIndex = g.length-1; + int sIndex = s.length - 1; + while (sIndex >= 0 && gIndex >= 0) { + + // 饼干可以分配,就让两个指针-- + if (s[sIndex] >= g[gIndex]) { + res++; + sIndex--; + gIndex--; + } else { + // 饼干不能分配,就分配给更小的孩子 + gIndex--; + } + } + + return res; + } + + + + + } + + +} diff --git a/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode53.java b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode53.java new file mode 100644 index 0000000..f56bd66 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode53.java @@ -0,0 +1,48 @@ +package cn.whaifree.leetCode.Greedy; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/25 10:53 + * @注释 + */ +public class LeetCode53 { + @Test + public void test() { + System.out.println(new LeetCode53().new Solution().maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4})); + } + + class Solution { + /** + * 遇到加上变为负数,重新从0计算。 + * 因为前面那一串会拖累后面那串 + * @param nums + * @return + */ + public int maxSubArray(int[] nums) { + int maxSum = Integer.MIN_VALUE; + int sum = 0; + for (int i = 0; i < nums.length; i++) { + // 加上某个数后是否会变为负数,会则直接从0开始计算,因为其只会拖累后面的串 + sum += nums[i]; + maxSum = Math.max(sum, maxSum); + if (sum < 0) { + sum = 0; + } + } + + + return maxSum; + } + } + + +} + diff --git a/src/main/java/cn/whaifree/leetCode/LeetCode001.java b/src/main/java/cn/whaifree/leetCode/LeetCode001.java deleted file mode 100644 index 3504933..0000000 --- a/src/main/java/cn/whaifree/leetCode/LeetCode001.java +++ /dev/null @@ -1,70 +0,0 @@ -package cn.whaifree.leetCode; - -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; - -/** - * - * 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标。 - * - * 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。 - * - * 你可以按任意顺序返回答案。 - * - * 来源:力扣(LeetCode) - * 链接:https://leetcode.cn/problems/two-sum - * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 - * - * - * - * 输入:nums = [2,7,11,15], target = 9 - * 输出:[0,1] - * 解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。 - * - * - * @author whaifree - * @package cn.whaifree.leetCode - * @Date: 2022/9/13 20:54 - */ -public class LeetCode001 { - - - - - /** - * map存储,key为nums内的值,value为下标 - * @param nums - * @param target - * @return - */ - public int[] twoSum(int[] nums, int target) { - - Map map = new HashMap<>(); - int key; - for (int i = 0; i < nums.length; i++) { - //如果包含匹配的元素即输出下标 - - key = target - nums[i]; - if (map.containsKey(key)){ - return new int[]{map.get(key),i}; - }else { - map.put(nums[i],i); - } - //不包含匹配的元素则增加到map中 - } - - return null; - - } - - public static void main(String[] args) { - int[] ints = new LeetCode001().twoSum(new int[]{2, 7, 11, 15}, 13); - for (int anInt : ints) { - System.out.println(anInt); - } - } - -} diff --git a/src/main/java/cn/whaifree/leetCode/Test.java b/src/main/java/cn/whaifree/leetCode/Test.java new file mode 100644 index 0000000..31c35f0 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Test.java @@ -0,0 +1,76 @@ +package cn.whaifree.leetCode; + +import cn.whaifree.leetCode.Tree.LeetCode94; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; +import java.util.PriorityQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +/** + * + * 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标。 + * + * 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。 + * + * 你可以按任意顺序返回答案。 + * + * 来源:力扣(LeetCode) + * 链接:https://leetcode.cn/problems/two-sum + * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 + * + * + * + * 输入:nums = [2,7,11,15], target = 9 + * 输出:[0,1] + * 解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。 + * + * + * @author whaifree + * @package cn.whaifree.leetCode + * @Date: 2022/9/13 20:54 + */ +public class Test { + + + static { + num = 20; + // 防止在初始化前程序中访问默认值 + } + private static int num = 10; + + public static void main(String[] args) { + ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); + System.out.println(systemClassLoader); + + ClassLoader extClassLoader = systemClassLoader.getParent(); + System.out.println(extClassLoader); + + // 获取不到引导类类加载器 + ClassLoader bootClassLoader = extClassLoader.getParent(); + System.out.println(bootClassLoader); + + ClassLoader classLoader = Test.class.getClassLoader(); + System.out.println(classLoader); + + + // 系统核心类库都是使用引导类(BootStrapClassLoader)进行加载,都为null + ClassLoader stringLoader = String.class.getClassLoader(); + System.out.println(stringLoader); + + + PriorityQueue objects = new PriorityQueue<>(); + objects.add(1); + objects.add(2); + objects.add(3); + objects.add(4); + System.out.println(objects.poll()); + System.out.println(objects.poll()); + + + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode94.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode94.java index 0261ecd..e8d2504 100644 --- a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode94.java +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode94.java @@ -14,6 +14,9 @@ import java.util.*; */ public class LeetCode94 { + + + @Test public void test() { TreeNode root = TreeNode.constructTree(new Integer[]{1, 2, 3, 4});