From 99bda3a447878c1124d92f73a273b1b4f6888a0c Mon Sep 17 00:00:00 2001 From: whai Date: Sat, 16 Mar 2024 19:02:55 +0800 Subject: [PATCH] redo --- .../leetCode/BackTracking/LeetCode698.java | 67 ++++++ .../cn/whaifree/leetCode/Dynamic/BeiBao.java | 123 ++++++++++- .../leetCode/Dynamic/LeetCode416.java | 207 ++++++++++++++++++ src/main/java/cn/whaifree/leetCode/Tes1.java | 44 ++++ .../redo/redo_24_3_16/LeetCode343.java | 42 ++++ .../redo/redo_24_3_16/LeetCode435.java | 59 +++++ .../redo/redo_24_3_16/LeetCode45.java | 45 ++++ .../redo/redo_24_3_16/LeetCode738.java | 46 ++++ .../redo/redo_24_3_16/LeetCode763.java | 51 +++++ .../redo/redo_24_3_16/LeetCode96.java | 41 ++++ .../redo/redo_24_3_16/LeetCode968.java | 64 ++++++ src/main/java/cn/whaifree/test/Custom.java | 13 ++ .../java/cn/whaifree/test/LambdaClass.java | 17 ++ 13 files changed, 818 insertions(+), 1 deletion(-) create mode 100644 src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode698.java create mode 100644 src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode416.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode343.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode435.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode45.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode738.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode763.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode96.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode968.java create mode 100644 src/main/java/cn/whaifree/test/Custom.java create mode 100644 src/main/java/cn/whaifree/test/LambdaClass.java diff --git a/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode698.java b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode698.java new file mode 100644 index 0000000..d1cc9ee --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode698.java @@ -0,0 +1,67 @@ +package cn.whaifree.leetCode.BackTracking; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/15 14:51 + * @注释 + */ +public class LeetCode698 { + + @Test + public void test() { + int[] nums = {4, 3, 2, 3, 5, 2, 1}; + int k = 4; + System.out.println(new Solution().canPartitionKSubsets(nums, k)); + } + + class Solution { + + public int[] numUsed; + public boolean canPartitionKSubsets(int[] nums, int k) { + numUsed = new int[nums.length]; + Arrays.sort(nums); + int sum = Arrays.stream(nums).sum(); + if (sum % k != 0 || nums[nums.length - 1] > sum / k) + return false; + + return divideGroups(nums, nums.length - 1, sum / k, 0, k); + } + + /** + * + * @param nums + * @param start + * @param target + * @param current + * @param k + * @return + */ + public boolean divideGroups(int[] nums, int start, int target, int current, int k) { + if (k == 1) + return true; // 分组操作执行k-1次之后,最后剩余的元素,就是最后一组了,不需要再匹配 + if (current == target) + return divideGroups(nums, nums.length - 1, target, 0, k - 1); // 分组操作执行k-1次后,最后剩余的元素,就是最后一组了,不需要再匹配 + for (int i = start; i >= 0; i--) { + if (numUsed[i] == 1 || current + nums[i] > target) continue; // 被使用的元素,不能再次使用;总和大于目标值,也不能使用 + + + numUsed[i] = 1; // 标记占用 + if (divideGroups(nums, i - 1, target, current + nums[i], k)) return true; + numUsed[i] = 0; // 撤销标记 + + + while (i > 0 && nums[i - 1] == nums[i]) i--; // 例如“12333333...”,假如最右侧的“3”这个值没有匹配上,那么它左侧的剩余五个“3”都不需要再匹配了。 + } + return false; + } + } + + +} diff --git a/src/main/java/cn/whaifree/leetCode/Dynamic/BeiBao.java b/src/main/java/cn/whaifree/leetCode/Dynamic/BeiBao.java index 49289bd..2c69c94 100644 --- a/src/main/java/cn/whaifree/leetCode/Dynamic/BeiBao.java +++ b/src/main/java/cn/whaifree/leetCode/Dynamic/BeiBao.java @@ -12,10 +12,29 @@ public class BeiBao { @Test public void main() { +// +// new Thread(() -> { +// try { +// Thread.sleep(1000); +// } catch (InterruptedException e) { +// throw new RuntimeException(e); +// } +// }).start(); +// +// new Thread(() -> { +// +// }).start(); +// +// +// String a = "a"; +// +// String b = new String("a"); +// +// System.out.println(a == b); int[] weight = {1,3,4}; int[] value = {15,20,30}; int bagSize = 4; - new Solution().packageProblem(weight, value, bagSize); + new Solution2().packageProblem(weight, value, bagSize); } class Solution{ @@ -81,4 +100,106 @@ public class BeiBao { } } + + + class Solution1{ + + /** + * dp[i][j] i表示商品,j表示空间 dp[i][j]表示 容量为j为包裹,从0-i个商品中取得最大值 + * + * 初始化 + * 0 1 2 3 4 5 包容量 + * 物品0 0 0 2 2 2 2 + * 物品1 0 + * 物品2 0 + * + * dp[i][j] 表示从下标为[0-i]的物品里任意取,放进容量为j的背包,价值总和最大是多少。 + * + * @param weights 物品的重量 + * @param values 物品的价值 + * @param carryNumber 可以携带的数量 + * @return + */ + public int packageProblem(int[] weights, int[] values, int carryNumber) { + int length = weights.length; + int[][] dp = new int[length][carryNumber + 1]; + for (int i = 0; i < length; i++) { + dp[i][0] = 0; + } + for (int i = weights[0]; i <= carryNumber; i++) { + dp[0][i] = values[0]; + } + + // 如果i 不放东西 dp[i][j] = dp[i-1][j] + // 如果i 放东西 dp[i][j] = max(dp[i-1][j],dp[i-1][j-weight[i]]-value[i]) + + // i为商品;j为容量。 + for (int i = 1; i < length; i++) { + for (int j = 1; j <= carryNumber; j++) { + if (weights[i] > j) { + // 重量太大,放不进去 + dp[i][j] = dp[i - 1][j]; + }else { + dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weights[i]] + values[i]); + } + } + } + + + return dp[length - 1][carryNumber]; + + } + } + + class Solution2{ + + /** + * + * dp[i][j] 表示从下标为[0-i]的物品里任意取,放进容量为j的背包,价值总和最大是多少。 + * dp[j] 表示容量为j的背包的最大价值总和 + * + * 初始化 + * 0 1 2 3 4 5 包容量 + * 物品0 0 0 2 2 2 2 + * 物品1 0 + * 物品2 0 + * + * 不放这个商品 那价值不变,还是dp[j] + * dp[j] = max(dp[j] , dp[j-weight[i]]+value[i]]) + * + * @param weights 物品的重量 + * @param values 物品的价值 + * @param carryNumber 可以携带的数量 + * @return + */ + public int packageProblem(int[] weights, int[] values, int carryNumber) { + int length = weights.length; + int[] dp = new int[carryNumber + 1]; + + // 不放这个商品 那价值不变,还是dp[j] + // dp[j] = max(dp[j] , dp[j-weight[i]]+value[i]]) + + // i为商品;j为容量。 + for(int i = 0; i < length; i++) { // 遍历物品 + for(int j = carryNumber ;j >= weights[i]; j--) { // 遍历背包容量 + dp[j] = Math.max(dp[j], dp[j - weights[i]] + values[i]); + } + /** + * 倒序遍历是为了保证物品i只被放入一次!。但如果一旦正序遍历了,那么物品0就会被重复加入多次! + * + * 举一个例子:物品0的重量weight[0] = 1,价值value[0] = 15 + * 如果正序遍历 + * dp[1] = dp[1 - weight[0]] + value[0] = 15 + * dp[2] = dp[2 - weight[0]] + value[0] = 30 + * + * 此时dp[2]就已经是30了,意味着物品0,被放入了两次,所以不能正序遍历。 + */ + } + + + return dp[carryNumber]; + + } + } + } diff --git a/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode416.java b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode416.java new file mode 100644 index 0000000..655342b --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode416.java @@ -0,0 +1,207 @@ +package cn.whaifree.leetCode.Dynamic; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/14 12:24 + * @注释 + */ +public class LeetCode416 { + + + @Test + public void test() { + int[] nums = {1,2,3,5}; + boolean canPartition = new Solution3().canPartition(nums); + System.out.println(canPartition); + } + +// class Solution { +// /** +// * 分割为2部分 +// * @param nums +// * @return +// */ +// public boolean canPartition(int[] nums) { +// Arrays.sort(nums); +// +// int rightSum = 0; +// for (int num : nums) { +// rightSum += num; +// } +// int leftSum = 0; +// for (int i = 0; i < nums.length; i++) { +// if (leftSum == rightSum) { +// return true; +// } else if (leftSum > rightSum) { +// return false; +// } +// leftSum += nums[i]; +// rightSum -= nums[i]; +// } +// return false; +// } +// } + + class Solution { + + int sumHalf = 0; + int nowSum = 0; + /** + * 回溯 + * + * @param nums + * @return + */ + public boolean canPartition(int[] nums) { + for (int num : nums) { + sumHalf += num; + } + sumHalf /= 2; + return backTracking(nums, 0); + } + + public boolean backTracking(int[] nums, int start) { + if (nowSum == sumHalf) { + return true; + }else if (nowSum>sumHalf){ + return false; + } + + boolean b = false; + for (int i = start; i < nums.length; i++) { + nowSum += nums[i]; + b = backTracking(nums, i + 1); + // 不行,有可能某些元素没在集合内 + nowSum -= nums[i]; + if (b==true) return true; + } + + return b; + } + } + + + class Solution2 { + /** + * 转换为背包问题 + * nums 为商品的重量、同时为商品的价值 + * 背包容量为sum/2 且需要装满 + * + * 0 1 2 3 4 5 + * 0 0 n0 n0 n0 n0 n0 + * 1 0 + * 2 0 + * 3 0 + * 4 0 + * 5 0 + * + * 不放东西的最大价值 dp[i][j] = dp[i-1][j] + * 放东西的最大价值 max(dp[i-1][j] , dp[i-1][j-nums[i]]+values[i]) + * + * @param nums + * @return + */ + public boolean canPartition(int[] nums) { + int sumHalf = 0; + for (int num : nums) { + sumHalf += num; + } + if (sumHalf % 2 != 0) { + return false; + } + sumHalf /= 2; + + int length = nums.length; + int[][] dp = new int[length][sumHalf + 1]; +// for (int i = 0; i < length; i++) { +// dp[i][0] = 0; +// } + for (int i = nums[0]; i <= sumHalf; i++) { + dp[0][i] = nums[0]; + } + + for (int i = 1; i < length; i++) { + for (int j = 1; j <= sumHalf; j++) { + if (nums[i] > j) { + // 放不下 + dp[i][j] = dp[i - 1][j]; + } else { + dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - nums[i]] + nums[i]); + } + + if (dp[i][j] == sumHalf) { + return true; + } + } + } + return false; + } + } + + class Solution3 { + /** + * 将nums的元素放入背包中 + * 1. 背包容量 sum/2 + * 2. 物品i重量nums[i];价值nums[i] + * + * 0 1 2 3 4 5 + * 0 + * 1 + * 2 + * + * dp[j]表示 j背包容量的最大价值 + * + * + * + * // 递推公式 + * 2. 放下 dp[j] = max(dp[j], dp[j-weight[i]]+nums[i]) + * + * 从后往前循环,每次取得状态不会和之前取得状态重合,这样每种物品就只取一次了。 + * + * @param nums + * @return + */ + public boolean canPartition(int[] nums) { + + int sum = 0; + for (int num : nums) { + sum += num; + } + + if (sum % 2 != 0) { + return false; + } + + // 包裹最大容量 + int packageCapacity = sum / 2; + + int[] dp = new int[packageCapacity + 1]; + + for (int i = 0; i < nums.length; i++) { + // 包的容量必须大于物品i重量才能放进去 + /** + * 如果正序遍历 + * dp[1] = dp[1 - weight[0]] + value[0] = 15 + * 此时容量1的背包已经放入了 + * dp[2] = dp[2 - weight[0]] + value[0] = 30 + * 此时又放入了一次 + */ + for (int j = packageCapacity; j >= nums[i]; j--) { + dp[j] = Math.max(dp[j], dp[j - nums[i]] + nums[i]); + //剪枝一下,每一次完成內層的for-loop,立即檢查是否dp[target] == target,優化時間複雜度(26ms -> 20ms) + if(dp[packageCapacity] == packageCapacity) + return true; + } + } + + // 包裹容量和目标值一样 + return dp[packageCapacity] == packageCapacity; + } + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/Tes1.java b/src/main/java/cn/whaifree/leetCode/Tes1.java index 5ac9bff..c316b5e 100644 --- a/src/main/java/cn/whaifree/leetCode/Tes1.java +++ b/src/main/java/cn/whaifree/leetCode/Tes1.java @@ -166,4 +166,48 @@ public class Tes1 { t2.start(); } + + } + +class Sun implements Inter,Outer { + + + public static void main(String[] args) { + new Sun().testDefault(); + Inter.testStatic(); + } + + /** + * 必须重写 + */ + @Override + public void testDefault() { + Outer.super.testDefault(); + Inter.super.testDefault(); + } +} + + +interface Inter{ + + default void testDefault(){ + System.out.println("testDefault"); + } + + static void testStatic(){ + System.out.println("testStatic"); + } +} + +interface Outer{ + + default void testDefault(){ + System.out.println("testDefault outer"); + } + + static void testStatic(){ + System.out.println("testStatic outer"); + } +} + diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode343.java b/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode343.java new file mode 100644 index 0000000..025c926 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode343.java @@ -0,0 +1,42 @@ +package cn.whaifree.redo.redo_24_3_16; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/16 16:59 + * @注释 + */ +public class LeetCode343 { + + class Solution { + + /** + * dp[j] 表示拆分出j后的乘积最大值 + * + * dp[j]= + * (j-i)*j 两个数相乘 或 + * dp[j-i] * i + * + * dp[j] = dp[j-1] * + * @param n + * @return + */ + public int integerBreak(int n) { + + int[] dp = new int[n + 1]; + dp[0] = 1; + dp[1] = 1; + dp[2] = 1; + for (int i = 3; i <= n; i++) { + for (int j = 1; j < i - 1; j++) { + dp[i] = Math.max( + dp[i], + Math.max((i - j) * j, dp[i - j] * j) + ); + } + } + + return dp[n]; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode435.java b/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode435.java new file mode 100644 index 0000000..85fab1b --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode435.java @@ -0,0 +1,59 @@ +package cn.whaifree.redo.redo_24_3_16; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.Comparator; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/16 17:42 + * @注释 + */ +public class LeetCode435 { + + @Test + public void test() { +// int[][] intervals = {{1,2},{2,3},{3,4},{1,3}}; +// int result = new Solution().eraseOverlapIntervals(intervals); +// System.out.println(result); +// +// +// // [ [1,2], [1,2], [1,2] ] +// int[][] intervals1 = {{1,2},{1,2},{1,2}}; +// int result1 = new Solution().eraseOverlapIntervals(intervals1); +// System.out.println(result1); + + // [[1,100],[11,22],[1,11],[2,12]] + int[][] intervals2 = {{1,100},{11,22},{1,11},{2,12}}; + int result2 = new Solution().eraseOverlapIntervals(intervals2); + System.out.println(result2); + } + + + class Solution { + public int eraseOverlapIntervals(int[][] intervals) { + // 最多能参加几次活动 + Arrays.sort(intervals, new Comparator() { + @Override + public int compare(int[] o1, int[] o2) { + return o1[0] - o2[0]; + } + }); + + int participation = 1; + for (int i = 1; i < intervals.length; i++) { + if (intervals[i][0] < intervals[i - 1][1]) { + // 后一个活动的开始时间早于头一个活动开始时间,这个活动不能参加 + intervals[i][1] = Math.min(intervals[i - 1][1], intervals[i][1]); + }else { + participation++; + } + } + + return intervals.length - participation; + + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode45.java b/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode45.java new file mode 100644 index 0000000..319d44a --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode45.java @@ -0,0 +1,45 @@ +package cn.whaifree.redo.redo_24_3_16; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/16 18:27 + * @注释 + */ +public class LeetCode45 { + + @Test + public void test() { + int[] nums = new int[]{2, 3, 1, 1, 4}; + int jump = new Solution().jump(nums); + System.out.println(jump); + } + + class Solution { + public int jump(int[] nums) { + if (nums.length == 1) { + return 0; + } + + int jump = 0; + int maxCover = 0; + int curCover = 0; // 用于标记是否达到一次跳的终点 + + for (int i = 0; i < nums.length; i++) { + maxCover = Math.max(maxCover, nums[i] + i); + if (i == curCover) { + jump++; + curCover = maxCover; + } + + if (curCover >= nums.length - 1) { + return jump; + } + } + + return jump; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode738.java b/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode738.java new file mode 100644 index 0000000..55a1e11 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode738.java @@ -0,0 +1,46 @@ +package cn.whaifree.redo.redo_24_3_16; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/16 18:04 + * @注释 + */ +public class LeetCode738 { + + @Test + public void test() { + Solution solution = new Solution(); + int n = 668799 ; + int n1 = 668799; + System.out.println(solution.monotoneIncreasingDigits(n)); + } + + class Solution { + public int monotoneIncreasingDigits(int n) { + + // 668841 668799 + + // 遍历的过程-1 ,再找到最后一个递减的然后 后面全部替换为9 + char[] chars = String.valueOf(n).toCharArray(); + int index = Integer.MAX_VALUE; + for (int i = chars.length - 1; i > 0; i--) { + if (chars[i] < chars[i - 1]) { + chars[i-1]--; + index = i; + } + } + + for (int i = index; i < chars.length ; i++) { + chars[i] = '9'; + } + + return Integer.parseInt(new String(chars)); + } + + } + + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode763.java b/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode763.java new file mode 100644 index 0000000..dbda280 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode763.java @@ -0,0 +1,51 @@ +package cn.whaifree.redo.redo_24_3_16; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/16 17:53 + * @注释 + */ +public class LeetCode763 { + + @Test + public void test() { + String s = "ababcbacadefegdehijhklij"; + Solution solution = new Solution(); + List list = solution.partitionLabels(s); + System.out.println(list); + } + + class Solution { + public List partitionLabels(String s) { + + // 统计每个字符最后出现的位置 + int[] map = new int[26]; + char[] chars = s.toCharArray(); + for (int i = 0; i < chars.length; i++) { + map[chars[i] - 'a'] = i; + } + + List list = new ArrayList<>(); + + int left = 0; + int rightMax = 0; + + for (int i = 0; i < chars.length; i++) { + rightMax = Math.max(map[chars[i] - 'a'], rightMax); + if (i == map[chars[i] - 'a'] && rightMax == i) { + list.add(rightMax - left + 1); + left = rightMax + 1; + } + } + + return list; + + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode96.java b/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode96.java new file mode 100644 index 0000000..67d0593 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode96.java @@ -0,0 +1,41 @@ +package cn.whaifree.redo.redo_24_3_16; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/16 16:40 + * @注释 + */ +public class LeetCode96 { + + @Test + public void test() { + int n = 4; + int result = new Solution().numTrees(n); + System.out.println(result); + } + + + class Solution { + public int numTrees(int n) { + if (n == 1) { + return 1; + } + //dp[i]表示i个节点的可能的树的数量 + int[] dp = new int[n + 1]; + dp[0] = 1; + dp[1] = 1; + dp[2] = 2; + + for (int i = 3; i <= n; i++) { + for (int j = 1; j <= i; j++) { + dp[i] += dp[i - j] * dp[j - 1]; + } + } + + return dp[n]; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode968.java b/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode968.java new file mode 100644 index 0000000..adff026 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_16/LeetCode968.java @@ -0,0 +1,64 @@ +package cn.whaifree.redo.redo_24_3_16; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/16 18:18 + * @注释 + */ +public class LeetCode968 { + + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTreeByArray(0,0,null,0,0); + System.out.println(new Solution().minCameraCover(treeNode)); + } + + class Solution { + int res = 0; + /** + * 0 被覆盖 + * 1 有监控 + * 2 没覆盖 + * + * @param root + * @return + */ + public int minCameraCover(TreeNode root) { + if (circle(root) == 2) { + res++; + } + return res; + } + + public int circle(TreeNode root) { + if (root == null) { + return 0; + } + + int left = circle(root.left); + int right = circle(root.right); + + // 左边或者右边有未覆盖的,就在这里安排一个监控 + if (left == 2 || right == 2) { + res++; + return 1; + } + + // 左边和右边有监控,这里就被覆盖了 + if (left == 1 || right == 1) { + return 0; + } + + // 左右都覆盖,这里就没覆盖 + if (left == 0 && right == 0) { + return 2; + } + + return -1; + } + } +} diff --git a/src/main/java/cn/whaifree/test/Custom.java b/src/main/java/cn/whaifree/test/Custom.java new file mode 100644 index 0000000..3fd48e5 --- /dev/null +++ b/src/main/java/cn/whaifree/test/Custom.java @@ -0,0 +1,13 @@ +package cn.whaifree.test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/14 14:58 + * @注释 + */ +@FunctionalInterface +public interface Custom { + void f(); +} + diff --git a/src/main/java/cn/whaifree/test/LambdaClass.java b/src/main/java/cn/whaifree/test/LambdaClass.java new file mode 100644 index 0000000..88bbb1d --- /dev/null +++ b/src/main/java/cn/whaifree/test/LambdaClass.java @@ -0,0 +1,17 @@ +package cn.whaifree.test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/14 14:58 + * @注释 + */ +public class LambdaClass { + public static void main(String[] args) { + new LambdaClass().lambdaInterfaceDemo(()-> System.out.println("自定义函数式接口")); + } + //函数式接口参数 + void lambdaInterfaceDemo(Custom i){ + i.f(); + } +}