From 9d0ddcf668a0f78e52c8072000c8ade1b0144524 Mon Sep 17 00:00:00 2001 From: whai Date: Tue, 26 Mar 2024 16:48:15 +0800 Subject: [PATCH] M src/main/java/cn/whaifree/interview/Meituan/MeiTuan24QiuZhao.java M src/main/java/cn/whaifree/interview/MiHaYou/Problem310.java M src/main/java/cn/whaifree/leetCode/Dynamic/AbsoluteBeiBao.java M src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode474.java M src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode494.java M src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode518.java M src/main/java/cn/whaifree/test/LambdaClass.java M src/main/java/cn/whaifree/test/testTry.java ?? hs_err_pid119104.log --- .../interview/Meituan/MeiTuan24QiuZhao.java | 82 ++++++++++++++ .../interview/MiHaYou/Problem310.java | 59 +++++++++- .../leetCode/Dynamic/AbsoluteBeiBao.java | 66 ++++++++++- .../leetCode/Dynamic/LeetCode474.java | 106 +++++++++++++++++- .../leetCode/Dynamic/LeetCode494.java | 59 ++++++++++ .../leetCode/Dynamic/LeetCode518.java | 87 +++++++++++++- .../java/cn/whaifree/test/LambdaClass.java | 78 ++++++++++++- src/main/java/cn/whaifree/test/testTry.java | 27 ++++- 8 files changed, 548 insertions(+), 16 deletions(-) diff --git a/src/main/java/cn/whaifree/interview/Meituan/MeiTuan24QiuZhao.java b/src/main/java/cn/whaifree/interview/Meituan/MeiTuan24QiuZhao.java index f115fc5..c4c793c 100644 --- a/src/main/java/cn/whaifree/interview/Meituan/MeiTuan24QiuZhao.java +++ b/src/main/java/cn/whaifree/interview/Meituan/MeiTuan24QiuZhao.java @@ -2,6 +2,8 @@ package cn.whaifree.interview.Meituan; import org.junit.Test; +import java.util.Arrays; +import java.util.HashMap; import java.util.Scanner; /** @@ -52,4 +54,84 @@ public class MeiTuan24QiuZhao { } } + + @Test + public void test1() { + Solution_2 solution_2 = new Solution_2(); + solution_2.max(new int[]{1,4,4}); + } + + class Solution_2{ + + public void max(int[] ints) { + // 统计每个数出现的数量 + // 遍历每个数i,判断i+1和i-1的数量是否>0,如果>0,就进行一次操作 i+1和i-1的数量-1,i的数量+2 + // 每经过一次操作,需要重新遍历 + HashMap map = new HashMap<>(); + for (int anInt : ints) { + Integer integer = map.get(anInt); + if (integer == null) { + map.put(anInt, 1); + }else { + map.put(anInt, integer + 1); + } + } + + map.forEach( + (k, v) -> { + System.out.println(k + ":" + v); + } + ); + + + + } + + } + + @Test + public void test2() { + Solution_3 solution_3 = new Solution_3(); + int[] ints = {1,0,0,0,1}; + solution_3.max(ints); + } + + class Solution_3{ + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + char[] chars = in.nextLine().toCharArray(); + // chars转为 ints + int[] ints = new int[chars.length]; + for (int i = 0; i < chars.length; i++) { + ints[i] = chars[i] - '0'; + } + max(ints); + } + + public static void max(int[] ints) { + + int res = 0; + for (int i = 0; i < ints.length; i++) { + for (int j = 1; i + j < ints.length; j++) { + res += change(Arrays.copyOf(ints, ints.length), i, i + j); + } + } + System.out.println(res); + } + + public static int change(int[] ints, int start, int end) { + int res = 0; + while (start < end ) { + if (ints[start] == ints[start + 1]) { + + res++; + ints[start + 1] = ints[start + 1] == 1 ? 0 : 1; + } + start++; + } + return res; + } + + } } diff --git a/src/main/java/cn/whaifree/interview/MiHaYou/Problem310.java b/src/main/java/cn/whaifree/interview/MiHaYou/Problem310.java index c8fce78..cec05da 100644 --- a/src/main/java/cn/whaifree/interview/MiHaYou/Problem310.java +++ b/src/main/java/cn/whaifree/interview/MiHaYou/Problem310.java @@ -1,9 +1,62 @@ -package cn.whaifree.interview.MiHaYou;/** +package cn.whaifree.interview.MiHaYou; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 * @Author whai文海 * @Date 2024/3/25 13:23 - * @version 1.0 * @注释 - * */ public class Problem310 { + + @Test + public void test() { + jumpSLM(3, new int[]{1, 0, 1}); + } + + + /** + * + * @param n n个格子 1-n + * @param direct 第i只史莱姆的跳跃方向 0表示往左,1表示往右 + */ + public void jumpSLM(int n, int[] direct) { + + int[] space = new int[n]; + Arrays.fill(space, 1); + + // 表示1-n秒 + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (direct[j] == 1) { + //右跳 + if (j + i + 1 < n) { + space[j + i] -= 1; + space[j + i + 1] += 1; + } else if (j + i + 1 == n) { + //边界条件 + space[j + i] -= 1; + } + }else { + //左跳 + if (j - (i + 1) >= 0) { + space[j - i] -= 1; + space[j - (i + 1)] += 1; + } else if (j - (i + 1) == -1) { + //边界条件 + space[j - i] -= 1; + } + } + } + System.out.println(Arrays.toString(space)); + } + + } + + + + } diff --git a/src/main/java/cn/whaifree/leetCode/Dynamic/AbsoluteBeiBao.java b/src/main/java/cn/whaifree/leetCode/Dynamic/AbsoluteBeiBao.java index b6e635a..60b03f4 100644 --- a/src/main/java/cn/whaifree/leetCode/Dynamic/AbsoluteBeiBao.java +++ b/src/main/java/cn/whaifree/leetCode/Dynamic/AbsoluteBeiBao.java @@ -1,9 +1,69 @@ -package cn.whaifree.leetCode.Dynamic;/** +package cn.whaifree.leetCode.Dynamic; + +import org.junit.Test; + +/** + * @version 1.0 * @Author whai文海 * @Date 2024/3/26 13:30 - * @version 1.0 * @注释 - * */ public class AbsoluteBeiBao { + + @Test + public void test() { + int[] weight = {1, 3, 4}; + int[] value = {15, 20, 30}; + int capacity = 4; + System.out.println(absoluteBeiBaoOneD(weight, value, capacity)); + } + + /** + * 使用动态规划解决背包问题,求解在给定背包容量下,能够获得的最大价值。 + * @param weight 物品的重量数组 + * @param value 物品的价值数组 + * @param capacity 背包的容量 + */ + public int absoluteBeiBao(int[] weight, int[] value,int capacity) { + + int length = weight.length; + int[][] dp = new int[length + 1][capacity + 1]; + + // 初始化动态规划数组 + // dp[i][j] 表示在前 i 个物品【0-(i-1)】中,背包容量为 j 的情况下的最大价值。 + for (int i = 1; i <= length; i++) { // + for (int j = 1; j <= capacity; j++) { + if (j >= weight[i-1]) { + // 当前物品重量小于等于背包容量时,考虑放入当前物品 + // 完全背包二维数组的代码跟一维只有下面一个下标不同, + // 那就是“放i”这个选择,因为是可以重复放的,所以是dp[i] + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - weight[i-1]] + value[i-1]); + }else { + // 当前物品重量大于背包容量,无法放入,维持前一个状态 + dp[i][j] = dp[i - 1][j]; + } + } + } + + return dp[length][capacity]; + } + + public int absoluteBeiBaoOneD(int[] weight, int[] value,int capacity) { + + int length = weight.length; + int[] dp = new int[capacity + 1]; + + // 初始化动态规划数组 + // dp[i][j] 表示在前 i 个物品【0-(i-1)】中,背包容量为 j 的情况下的最大价值。 + // dp[j] 表示前i个物品中,背包容量为j的最大价值 + for (int i = 1; i <= length; i++) { // + for (int j = weight[i - 1]; j <= capacity; j++) { + dp[j] = Math.max(dp[j], dp[j - weight[i - 1]] + value[i - 1]); + } + } + return dp[capacity]; + } + + + } diff --git a/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode474.java b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode474.java index b858516..78de04f 100644 --- a/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode474.java +++ b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode474.java @@ -1,9 +1,109 @@ -package cn.whaifree.leetCode.Dynamic;/** +package cn.whaifree.leetCode.Dynamic; + +import org.junit.Test; + +/** + * @version 1.0 * @Author whai文海 * @Date 2024/3/26 11:49 - * @version 1.0 * @注释 - * */ public class LeetCode474 { + + @Test + public void test() + { + String[] strs = {"10","0001","111001","1","0"}; + int m = 5; + int n = 3; +// System.out.println(new Solution().findMaxForm(strs, m, n)); + // "10", "0", "1" + strs = new String[]{"10","0","1"}; + System.out.println(new Solution1().findMaxForm(strs, 1, 1)); + + } + class Solution { + public int findMaxForm(String[] strs, int m, int n) { + + // 三维 dp[i][j][k] 表示在前i个字符串中,包含了了j个0和k个1的最大子集长度 + // String[i]为物品 j k 为背包 + // 初始化: 当物品i=0 ,表示在前0个字符串中,没有字符串可以使用,dp[0][j][k]=0 + // 递推公式: + // 获取0的数量numZero 获取1的数量numOne + // 当j=zeroNumber + int[][] dp = new int[m + 1][n + 1]; + + for (String str : strs) { + int zeroNumber = calculateZeroNumber(str); + int oneNumber = str.length() - zeroNumber; + for (int j = m; j >= zeroNumber; j--) { + for (int k = n; k >= oneNumber; k--) { + dp[j][k] = Math.max(dp[j - zeroNumber][k - oneNumber] + 1, dp[j][k]); + } + } + } + + return dp[m][n]; + } + + public int calculateZeroNumber(String str) + { + int num = 0; + for (int i = 0; i < str.length(); i++) { + if (str.charAt(i) == '0') + { + num++; + } + } + return num; + } + } } diff --git a/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode494.java b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode494.java index 691cb23..32c4260 100644 --- a/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode494.java +++ b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode494.java @@ -169,5 +169,64 @@ public class LeetCode494 { } } + class Solution3 { + /** + * 查找目标和的方法数 + * 给定一个整数数组 nums 和一个目标整数 target,求出可以通过从数组中选择数字并进行加法运算得到目标和的方法数。 + * 数组中的每个数字可以被选择任意次数。 + * + * 其中 dp[i][j] 表示在数组 nnums 的前 i 个数中选取元素,使得这些元素之和等于 j 的方案数 + * + * + * @param nums 整数数组,包含要进行加法运算的整数 + * @param target 目标整数,要达到的和 + * @return 返回可以通过选择数组中的数字并进行加法运算得到目标和的方法数 + */ + public int findTargetSumWays(int[] nums, int target) { + // 计算数组 nums 中所有数字的和 + int sum = 0; + for (int num : nums) { + sum += num; + } + // 计算达到目标和需要减去的数 + int diff = sum - target; + // 如果差值小于0或者差值为奇数,则无法达到目标和,返回0 + if (diff < 0 || diff % 2 != 0) { + return 0; + } + // 计算可以取的负数的个数 + int n = nums.length; + int neg = diff / 2; + // 使用动态规划的二维数组,dp[i][j] 表示前 i 个数字中选取和为 j 的方法数 + int[][] dp = new int[n + 1][neg + 1]; + // 初始化,当不选取任何数字时,和为 0 的方法数为 1 + dp[0][0] = 1; + // 遍历数组 nums,更新 dp 数组 + for (int i = 1; i <= n; i++) { + int num = nums[i - 1]; + for (int j = 0; j <= neg; j++) { + // 不选择当前数字 num + dp[i][j] = dp[i - 1][j]; + // 选择当前数字 num + if (j >= num) { + dp[i][j] += dp[i - 1][j - num]; + } + } + } + // 返回最后一个数字选取和为 neg 的方法数 + return dp[n][neg]; + } + + } + + + @Test + public void test1() + { + int[] nums = {1,1,1,1,1}; + int target = 3; + System.out.println(new Solution3().findTargetSumWays(nums, target)); + } + } diff --git a/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode518.java b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode518.java index 3043f5f..378414a 100644 --- a/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode518.java +++ b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode518.java @@ -1,9 +1,90 @@ -package cn.whaifree.leetCode.Dynamic;/** +package cn.whaifree.leetCode.Dynamic; + +import org.junit.Test; + +/** + * @version 1.0 * @Author whai文海 * @Date 2024/3/26 15:37 - * @version 1.0 * @注释 - * */ public class LeetCode518 { + + @Test + public void test() { + int[] coins = {1, 2, 5}; + int amount = 5; + int i = new Solution2().change(amount, coins); + System.out.println(i); + } + + class Solution { + /** + * dp[j] 表示前i个硬币容量为j的背包能够装入的组合数 + * + * dp[j] = dp[j-coins[i-1]] + 1 + * + * @param amount + * @param coins + * @return + */ + public int change(int amount, int[] coins) { + + + int[] dp = new int[amount + 1]; + + dp[0] = 1; + for (int i = 1; i <= coins.length; i++) { + for (int j = coins[i - 1]; j <= amount; j++) { + dp[j] = dp[j] + dp[j - coins[i - 1]] ; + } + } + + return dp[amount]; + + } + } + + class Solution2{ + + /** + * 二维数组 + * @param amount + * @param coins + * @return + */ + public int change(int amount, int[] coins) { + int n = coins.length; + int[][] dp = new int[n + 1][amount + 1]; + + // 初始化第一行,即不使用任何硬币时,只有金额为0的方法数为1 + for (int j = 0; j <= amount; j++) { + dp[0][j] = 0; + } + dp[0][0] = 1; + + // 初始化第一列,即凑齐金额为0的方法数都是1(不使用任何硬币) + for (int i = 0; i <= n; i++) { + dp[i][0] = 1; + } + + // 动态规划填表 + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= amount; j++) { + // 不使用第i种硬币 + dp[i][j] = dp[i - 1][j]; + + // 如果当前金额j大于等于第i种硬币的面值,那么可以考虑使用这种硬币 + // 使用第i种硬币的方法数等于不使用第i种硬币的方法数加上使用第i种硬币后的方法数 + if (j >= coins[i - 1]) { + dp[i][j] += dp[i][j - coins[i - 1]]; + } + } + } + + return dp[n][amount]; + } + } + + } diff --git a/src/main/java/cn/whaifree/test/LambdaClass.java b/src/main/java/cn/whaifree/test/LambdaClass.java index 88bbb1d..ee84e24 100644 --- a/src/main/java/cn/whaifree/test/LambdaClass.java +++ b/src/main/java/cn/whaifree/test/LambdaClass.java @@ -1,5 +1,10 @@ package cn.whaifree.test; +import java.util.Collection; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + /** * @version 1.0 * @Author whai文海 @@ -8,10 +13,81 @@ package cn.whaifree.test; */ public class LambdaClass { public static void main(String[] args) { - new LambdaClass().lambdaInterfaceDemo(()-> System.out.println("自定义函数式接口")); + + + + + +// new LambdaClass().lambdaInterfaceDemo(()-> System.out.println("自定义函数式接口")); + } //函数式接口参数 void lambdaInterfaceDemo(Custom i){ i.f(); } } + + +class User { + int age; + String name; + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + + if (obj == null || getClass() != obj.getClass()) { + return false; + } + + + User other = (User) obj; + if (age == other.age && other.name.equals(name)) { + return true; + } + return false; + } + + @Override + public int hashCode() { + return Objects.hash(age, name); + } + +} + +class CustomSet extends HashSet { + + + + + @Override + public boolean equals(Object o) + { + + if (o == this) // 同一个对象 + return true; + if (!(o instanceof Set)) // 不是Set接口的实现 + return false; + + Collection c = (Collection) o; + if (c.size() != size()) // 大小不一样 + return false; + try { + //挨个遍历,是否存在e + for (Object e : c){ + if (!contains(e)) + return false; + } + return true; + } catch (ClassCastException | NullPointerException unused) { + return false; + } + } + + @Override + public int hashCode() { + return super.hashCode(); + } +} diff --git a/src/main/java/cn/whaifree/test/testTry.java b/src/main/java/cn/whaifree/test/testTry.java index 6350b91..48c3306 100644 --- a/src/main/java/cn/whaifree/test/testTry.java +++ b/src/main/java/cn/whaifree/test/testTry.java @@ -1,9 +1,30 @@ -package cn.whaifree.test;/** +package cn.whaifree.test; + +import org.junit.Test; + +/** + * @version 1.0 * @Author whai文海 * @Date 2024/3/24 22:42 - * @version 1.0 * @注释 - * */ public class testTry { + + @Test + public void test() { + int i = tryTest(); + System.out.println(i); + } + + public int tryTest() { + + try { + return 1; + } catch (Exception e) { + + }finally { + return 2; + } + } + }