diff --git a/src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode1143.java b/src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode1143.java new file mode 100644 index 0000000..cd84040 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode1143.java @@ -0,0 +1,51 @@ +package cn.whaifree.redo.redo_24_4_27; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/5/1 15:39 + * @注释 + */ +public class LeetCode1143 { + @Test + public void test() { + System.out.println(new Solution().longestCommonSubsequence("abcde", "ace")); + } + + class Solution { + public int longestCommonSubsequence(String text1, String text2) { + /** + * dp[i][j] 表示t1的0-i t2的0-j的公共子序列的长度 + * '' a b c d e + *'' 0 0 0 0 0 0 + * a 0 1 1 1 1 1 + * c 0 1 1 2 2 2 + * e 0 1 1 2 2 3 + * + * if t1[i]==t2[j] + * dp[i-1][j-1]+1 + * else + * dp[i-1][j] + * + */ + int len2 = text2.length(); + int len1 = text1.length(); + int[][] dp = new int[len2 + 1][len1 + 1]; + + for (int i = 1; i < len2 + 1; i++) { + for (int j = 1; j < len1 + 1; j++) { + if (text1.charAt(j - 1) == text2.charAt(i - 1)) { + dp[i][j] = dp[i - 1][j - 1] + 1; + } else { + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); + // 取前面已经匹配过的最大长度 + } + } + } + return dp[len2][len1]; + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode392.java b/src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode392.java new file mode 100644 index 0000000..b101df7 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode392.java @@ -0,0 +1,68 @@ +package cn.whaifree.redo.redo_24_4_27; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/5/1 12:17 + * @注释 + */ +public class LeetCode392 { + + @Test + public void test() { + + String s = "agc"; + String t = "ahbgdc"; + boolean subsequence = new Solution().isSubsequence(s, t); + System.out.println(subsequence); + } + + class Solution { + /** + * + * dp[i][j] 表示 s 的0-i是否 在 t的子序列中出现的长度 + * + * '' a h b g d c + * '' 0 0 0 0 0 0 0 + * a 0 1 1 1 1 1 1 不相等 左边 相等 i-1 j-1 +1 + * b 0 0 0 2 2 2 2 + * c 0 0 0 0 0 0 3 + * + * @param s + * @param t + * @return + */ + public boolean isSubsequence(String s, String t) { + + int[][] dp = new int[s.length() + 1][t.length() + 1]; + + for (int i = 1; i < s.length() + 1; i++) { + for (int j = 1; j < t.length()+1; j++) { + if (s.charAt(i - 1) == t.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1] + 1; + }else { + dp[i][j] = dp[i][j - 1]; + } + } + } + return s.length() == dp[s.length()][t.length()]; + } + + } + + class Solution1 { + public boolean isSubsequence(String s, String t) { + int indexS = 0; + int indexT = 0; + while (indexS < s.length() && indexT < t.length()) { + if (s.charAt(indexS) == t.charAt(indexT)) { + indexS++; + } + indexT++; + } + return indexS == s.length(); + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode674.java b/src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode674.java new file mode 100644 index 0000000..afe656b --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode674.java @@ -0,0 +1,44 @@ +package cn.whaifree.redo.redo_24_4_27; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/5/1 12:59 + * @注释 + */ +public class LeetCode674 { + @Test + public void test() { + Solution solution = new Solution(); + int[] nums = {1}; + int lengthOfLCIS = solution.findLengthOfLCIS(nums); + System.out.println(lengthOfLCIS); + } + + class Solution { + public int findLengthOfLCIS(int[] nums) { + /** + * dp[i] 表示以i结尾的最长递增子序列 + * + * 1 3 5 4 7 + * 1 2 3 0 1 + */ + + int[] dp = new int[nums.length + 1]; + int max = 1; + dp[0] = 1; + for (int i = 1; i < nums.length; i++) { + if (nums[i] <= nums[i - 1]) { + dp[i] = 1; + }else { + dp[i] = dp[i - 1] + 1; + } + max = Math.max(max, dp[i]); + } + return max; + } + + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode718.java b/src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode718.java new file mode 100644 index 0000000..f90efd2 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode718.java @@ -0,0 +1,53 @@ +package cn.whaifree.redo.redo_24_4_27; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/5/1 13:06 + * @注释 + */ +public class LeetCode718 { + @Test + public void test() + { + Solution solution = new Solution(); + int[] nums1 = new int[]{1,2,3,2,1}; + int[] nums2 = new int[]{3,2,1,4,7}; + int length = solution.findLength(nums2, nums1); + System.out.println(length); + } + + class Solution { + public int findLength(int[] nums1, int[] nums2) { + /** + * 子数组 连续==>遇到不相等要重置 + * dp[i][j]表示以i-1 j-1为结尾的最长子数组长度 + * '' 1, 2, 3, 2, 1 + * '' [0, 0, 0, 0, 0, 0], + * 3 [0, 0, 0, 1, 0, 0], + * 2 [0, 0, 1, 0, 2, 0], + * 1 [0, 1, 0, 0, 0, 3], + * 4 [0, 0, 0, 0, 0, 0], + * 7 [0, 0, 0, 0, 0, 0] + * + * 相等 取i-1 j-1 + 1 + * + */ + int[][] dp = new int[nums1.length + 1][nums2.length + 1]; + int max = 0; + for (int i = 1; i < nums1.length + 1; i++) { + for (int j = 1; j < nums2.length + 1; j++) { + if (nums1[i - 1] == nums2[j - 1]) { + dp[i][j] = dp[i - 1][j - 1] + 1; + }else { + dp[i][j] = 0; + } + max = Math.max(max, dp[i][j]); + } + } + return max; + } + } +} diff --git a/src/main/java/cn/whaifree/test/FutureTask.java b/src/main/java/cn/whaifree/test/FutureTask.java new file mode 100644 index 0000000..95f8674 --- /dev/null +++ b/src/main/java/cn/whaifree/test/FutureTask.java @@ -0,0 +1,133 @@ +package cn.whaifree.test; + +import java.util.concurrent.Callable; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.function.BiFunction; +import java.util.function.Supplier; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/4/30 17:14 + * @注释 + */ +public class FutureTask { + + public static void main(String[] args) throws ExecutionException, InterruptedException { + // 创建一个异步任务,该任务返回一个整数 + CompletableFuture future = CompletableFuture.supplyAsync( + new Supplier() { + @Override + public Integer get() { + System.out.println("A complete"); + return 10; + } + } + ); + + // 创建另一个异步任务,该任务同样返回一个整数 + CompletableFuture future2 = CompletableFuture.supplyAsync( + () -> { + System.out.println("B start"); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + System.out.println("B complete"); + return 20; + } + ); + + // 两个异步任务完成计算后,将它们的结果相加 + CompletableFuture ans = future.thenCombine(future2, new BiFunction() { + @Override + public Object apply(Integer integer, Integer integer2) { + return integer + integer2; + } + } + ); + + // 打印结果 + System.out.println(ans.get()); + + + // callable创建线程 + java.util.concurrent.FutureTask futureTask = new java.util.concurrent.FutureTask<>(new Callable() { + @Override + public Integer call() throws Exception { + // 暂停100毫秒 + Thread.sleep(100); + return 100; + } + }); + + + + // 启动线程 + new Thread(futureTask).start(); + + + + // 等待futureTask完成 + while (!futureTask.isDone()) { + Thread.sleep(50); + System.out.println("no done"); + } + // 输出结果 + System.out.println(futureTask.get()); + + + } +} +class Join{ + public static void main(String[] args) { + + Thread thread = new Thread(() -> { + try { + System.out.println("A Start"); + Thread.sleep(10000); + System.out.println("A DONE"); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + }); + thread.start(); + + new Thread(() -> { + try { + thread.join(); // 等待A线程执行完成 + System.out.println("B DONE"); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + }).start(); + } +} + +class local{ + + // 创建一个线程局部变量threadLocal1,并设置其初始值为10 + private static ThreadLocal threadLocal1 = ThreadLocal.withInitial(() -> 10); + // 创建一个线程局部变量threadLocal2,并设置其初始值为"Hello" + private static ThreadLocal threadLocal2 = ThreadLocal.withInitial(() -> "Hello"); + + public static void main(String[] args) { + // 创建一个新线程thread,并为其指定一个Runnable实现 + Thread thread = new Thread(() -> { + // 获取threadLocal1的值 + int value1 = threadLocal1.get(); // 放入Thread的Map key 为ThreadLocal value为10 + // 获取threadLocal2的值 + String value2 = threadLocal2.get(); // 放入Thread的Map key 为ThreadLocal value为Hello + // 打印threadLocal1的值 + System.out.println("ThreadLocal1: " + value1); + // 打印threadLocal2的值 + System.out.println("ThreadLocal2: " + value2); + }); + + // 启动线程thread + thread.start(); + } +}