diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/Array/LeetCode86.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/Array/LeetCode86.java new file mode 100644 index 0000000..e45fbaf --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/Array/LeetCode86.java @@ -0,0 +1,48 @@ +package cn.whaifree.leetCode.Array; + +import cn.whaifree.leetCode.model.ListNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/6 12:45 + * @注释 + */ +public class LeetCode86 { + + @Test + public void test() { + ListNode listNode = ListNode.listNodeFromArray(new int[]{1, 4, 3, 2, 5, 2}); + ListNode listNode1 = new Solution().partition(listNode, 3); + listNode1.printList(); + } + + /** + * + */ + + class Solution { + public ListNode partition(ListNode head, int x) { + ListNode pre = new ListNode(-1, head); + ListNode small = new ListNode(-1); + ListNode big = new ListNode(-1); + ListNode smallIndex = small; + ListNode bigIndex = big; + ListNode index = head; + while (index != null) { + if (index.val < x) { + smallIndex.next = index; + smallIndex = smallIndex.next; + } else { + bigIndex.next = index; + bigIndex = bigIndex.next; + } + index = index.next; + } + smallIndex.next = big.next; + bigIndex.next = null; + return small.next; + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode139.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode139.java index 2503cec..447d4dd 100644 --- a/ForJdk17/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode139.java +++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode139.java @@ -1,5 +1,7 @@ package cn.whaifree.leetCode.Dynamic; +import org.junit.Test; + import java.util.List; /** @@ -10,22 +12,73 @@ import java.util.List; */ public class LeetCode139 { + @Test + public void test() { +// System.out.println(new Solution().wordBreak("leetcode", List.of("leet", "code"))); +// System.out.println(new Solution().wordBreak("leetcode", List.of("leet", "cod", "e"))); + // ["apple","pen"] + System.out.println(new Solution().wordBreak("applepenapple", List.of("apple", "pen"))); + + } + class Solution { /** + * + * dp[i][j] 表示s中前j个字母能否被0-i中任意元素组合出来 + * + * 定义 dp[i] 表示字符串 s 前 i 个字符组成的字符串 s[0..i−1] 是否能被空格拆分成若干个字典中出现的单词 + * + * c a t s a n d o g + * cats 0 0 0 1 0 + * dog + * sand + * and + * cat + * '' l e e t c o d e + * '' 1 0 0 0 0 0 0 0 0 + * leet 1 0 0 0 1 0 0 0 0 + * code 1 0 0 0 0 0 0 0 1 + * + * a p p l e p e n a p p l e + * apple 1 0 0 0 0 1 0 0 0 0 0 0 0 1 + * pen 1 0 0 0 0 1 0 0 1 0 0 0 0 0 * * @param s * @param wordDict * @return */ public boolean wordBreak(String s, List wordDict) { - for (int i = 0; i < s.length(); i++) { - + // 完全背包,先便利字符串再遍历背包,确保重会重复选择背包 + int len = s.length(); + boolean[] dp = new boolean[len + 1]; + dp[0] = true; + for (int j = 1; j <= len; j++) { + for (int i = 0; i < wordDict.size(); i++) { + String str = wordDict.get(i); + int index = j - str.length(); + if (index >= 0 && dp[index] && check(s, j - 1, str)) { + dp[j] = true; + } + } } - - return true; + return dp[len]; } + + public boolean check(String s, int start, String word) { + for (int i = word.length() - 1; i >= 0; i--) { + if (word.charAt(i) != s.charAt(start)) { + return false; + } + start--; + } + return true; + } + } + @Test + public void main() { + System.out.println(new Solution().check("leetcode", 7, "code")); } } diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode138.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode138.java new file mode 100644 index 0000000..2b9b967 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode138.java @@ -0,0 +1,84 @@ +package cn.whaifree.redo.redo_all_240924; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/7 11:37 + * @注释 + */ +public class LeetCode138 { + class Node { + int val; + Node next; + Node random; + + public Node(int val) { + this.val = val; + this.next = null; + this.random = null; + } + } + + @Test + public void test() { + Node node1 = new Node(7); + Node node2 = new Node(13); + Node node3 = new Node(11); + Node node4 = new Node(10); + Node node5 = new Node(1); + + node1.next = node2; + node2.next = node3; + node3.next = node4; + node4.next = node5; + + node2.random = node1; + node3.random = node5; + node4.random = node3; + node5.random = node1; + + Node res = new Solution().copyRandomList(node1); + System.out.println(res); + } + + class Solution { + public Node copyRandomList(Node head) { + Node pre = new Node(-1); + pre.next = head; + Node index = pre.next; + while (index!= null) { + Node tmp = index.next; + Node newNode = new Node(index.val); + index.next = newNode; + newNode.next = tmp; + index = index.next.next; + } + + index = pre.next; + while (index != null) { + Node nowRandom = index.random; + if (nowRandom == null) { + index.next.random = null; + }else { + index.next.random = nowRandom.next; + } + index = index.next.next; + } + + Node newHead = new Node(-1); + Node newHeadIndex = newHead; + Node idx = pre.next; + while (idx != null) { + newHeadIndex.next = idx.next; + newHeadIndex = newHeadIndex.next; + idx.next = idx.next.next; + idx = idx.next; + } + + return newHead.next; + } + } + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/tmp/Thread/T241006/ThreadChangePrintDemo.java b/ForJdk17/src/main/java/cn/whaifree/tmp/Thread/T241006/ThreadChangePrintDemo.java new file mode 100644 index 0000000..c9ff91e --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/tmp/Thread/T241006/ThreadChangePrintDemo.java @@ -0,0 +1,166 @@ +package cn.whaifree.tmp.Thread.T241006; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Semaphore; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/6 19:20 + * @注释 + */ +public class ThreadChangePrintDemo { + + +} +class DownLatch{ + + public static void main(String[] args) { + CountDownLatch countDownLatch = new CountDownLatch(99); + + new Thread(() -> { + for (int i = 0; i < 30; i++) { + while (countDownLatch.getCount() % 3 != 0) {} + System.out.println("A"); + countDownLatch.countDown(); + } + }).start(); + + new Thread(() -> { + for (int i = 0; i < 30; i++) { + while (countDownLatch.getCount() % 3 != 2) {} + System.out.println("B"); + countDownLatch.countDown(); + } + }).start(); + + new Thread(new Runnable() { + @Override + public void run() { + for (int i = 0; i < 30; i++) { + while (countDownLatch.getCount() % 3 != 1) {} + System.out.println("C"); + countDownLatch.countDown(); + } + } + }).start(); + } +} + +class SemaphoreDemo{ + + static Semaphore s1 = new Semaphore(1); + static Semaphore s2 = new Semaphore(0); + static Semaphore s3 = new Semaphore(0); + + public static void main(String[] args) { + + new Thread(new Runnable() { + @Override + public void run() { + try { + for (int i = 0; i < 30; i++) { + s1.acquire(); + System.out.println("A"); + s2.release(); + } + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + }).start(); + + new Thread(new Runnable() { + @Override + public void run() { + try { + for (int i = 0; i < 30; i++) { + s2.acquire(); + System.out.println("B"); + s3.release(); + } + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + }).start(); + + new Thread(new Runnable() { + @Override + public void run() { + try { + for (int i = 0; i < 30; i++) { + s3.acquire(); + System.out.println("C"); + s1.release(); + } + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + }).start(); + } + +} + + +class Syn{ + static Object o = new Object(); + static int count = 0; + public static void main(String[] args) { + + new Thread(() -> { + try { + for (int i = 0; i < 100; i++) { + synchronized (o) { + while (count % 3 != 0) { + o.wait(); + } + System.out.println("A"); + o.notifyAll(); + count++; + } + + } + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + }).start(); + + new Thread(() -> { + try { + for (int i = 0; i < 100; i++) { + synchronized (o) { + while (count % 3 != 1) { + o.wait(); + } + System.out.println("B"); + o.notifyAll(); + count++; + } + + } + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + }).start(); + + new Thread(() -> { + try { + for (int i = 0; i < 100; i++) { + synchronized (o) { + while (count % 3 != 2) { + o.wait(); + } + System.out.println("C"); + o.notifyAll(); + count++; + } + + } + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + }).start(); + } +}