feat: 添加了LeetCode86, LeetCode139, ThreadChangePrintDemo, Syn和LeetCode138的解决方案

Default Changelist
LeetCode86.java
LeetCode138.java
LeetCode139.java
ThreadChangePrintDemo.java
This commit is contained in:
whaifree 2024-10-07 23:16:25 +08:00
parent fd440507c4
commit 3789fe03e3
4 changed files with 355 additions and 4 deletions

View File

@ -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;
}
}
}

View File

@ -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..i1] 是否能被空格拆分成若干个字典中出现的单词
*
* 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<String> 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 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"));
}
}

View File

@ -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;
}
}
}

View File

@ -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();
}
}