修复了LeetCode206的测试用例,优化了代码结构
This commit is contained in:
parent
aba574133e
commit
ed7b36aa7c
@ -0,0 +1,40 @@
|
||||
package cn.whaifree.redo.redo_all_240511;
|
||||
|
||||
import cn.whaifree.leetCode.model.ListNode;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/5/24 19:28
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode206 {
|
||||
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
ListNode listNode = ListNode.listNodeFromArray(new int[]{1, 2, 3, 4, 5});
|
||||
listNode.printList();
|
||||
ListNode listNode1 = new Solution().reverseList(listNode);
|
||||
listNode1.printList();
|
||||
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public ListNode reverseList(ListNode head) {
|
||||
ListNode reverse = reverse(null, head);
|
||||
return reverse;
|
||||
}
|
||||
|
||||
public ListNode reverse(ListNode pre, ListNode after) {
|
||||
if (after == null) {
|
||||
return pre;
|
||||
}
|
||||
ListNode next = after.next;
|
||||
after.next = pre;
|
||||
ListNode reverse = reverse(after, next);
|
||||
return reverse;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package cn.whaifree.redo.redo_all_240511;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/5/16 20:36
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode647 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
System.out.println(new Solution1().countSubstrings("a"));
|
||||
}
|
||||
|
||||
|
||||
class Solution1 {
|
||||
public int countSubstrings(String s) {
|
||||
char[] c = s.toCharArray();
|
||||
int sum = 0;
|
||||
for (int i = 0; i < c.length; i++) {
|
||||
sum += count(c, i, i);
|
||||
sum += count(c, i, i + 1);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
public int count(char[] c, int start, int end) {
|
||||
int count = 0;
|
||||
while (start >= 0 && end < c.length && c[start] == c[end]) {
|
||||
start--;
|
||||
end++;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Solution {
|
||||
|
||||
/**
|
||||
* dp[i][j] 表示i-j回文子串的个数
|
||||
* i-j 可以由 i-1 j+1 推出 所以必须先从右到左推
|
||||
* if dp[i+1][j-1] && s[i]==s[j] || i==j i+1=j
|
||||
* dp[i][j] = true
|
||||
* else
|
||||
* dp[i][j] = false
|
||||
*
|
||||
* @param s
|
||||
* @return
|
||||
*/
|
||||
public int countSubstrings(String s) {
|
||||
int size = s.length();
|
||||
boolean[][] dp = new boolean[size + 1][size + 1];
|
||||
int count = 0;
|
||||
for (int i = size; i > 0; i--) {
|
||||
for (int j = i; j <= size; j++) {
|
||||
if (i == j || ((i == j - 1 || dp[i + 1][j - 1]) && s.charAt(i - 1) == s.charAt(j - 1))) {
|
||||
dp[i][j] = true;
|
||||
}
|
||||
if (dp[i][j]) count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
23
src/main/java/cn/whaifree/redo/redo_all_240511/Line.java
Normal file
23
src/main/java/cn/whaifree/redo/redo_all_240511/Line.java
Normal file
@ -0,0 +1,23 @@
|
||||
package cn.whaifree.redo.redo_all_240511;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/5/15 22:06
|
||||
* @注释
|
||||
*/
|
||||
public class Line {
|
||||
|
||||
public static void main(String[] args) throws FileNotFoundException {
|
||||
List<String> s = FileUtil.readLines(new File("D:\\project\\LeetCode\\src\\main\\java\\cn\\whaifree\\redo\\redo_all_240511\\README.md"), Charset.defaultCharset());
|
||||
int line = (int) s.stream().filter(i -> !i.trim().isEmpty()).count();
|
||||
System.out.println(line);
|
||||
}
|
||||
}
|
236
src/main/java/cn/whaifree/redo/redo_all_240511/README.md
Normal file
236
src/main/java/cn/whaifree/redo/redo_all_240511/README.md
Normal file
@ -0,0 +1,236 @@
|
||||
[LeetCode206_23_12_28^递归.md](md笔记\LeetCode206_23_12_28^递归.md)
|
||||
|
||||
[LeetCode287_24_1_1^.md](md笔记\LeetCode287_24_1_1^.md)
|
||||
|
||||
[LeetCode142_24_1_1^.md](md笔记\LeetCode142_24_1_1^.md)
|
||||
|
||||
[LeetCode202_24_1_2^.md](md笔记\LeetCode202_24_1_2^.md)
|
||||
|
||||
[LeetCode2487_24_1_3^.md](md笔记\LeetCode2487_24_1_3^.md)
|
||||
|
||||
[LeetCode15_24_1_3^.md](md笔记\LeetCode15_24_1_3^.md)
|
||||
[LeetCode18_24_1_4^.md](2024-1月-before-7\LeetCode18_24_1_4^.md)
|
||||
|
||||
[easyLeetCode28_24_1_5^](md笔记\LeetCode28_24_1_5^)
|
||||
|
||||
[LeetCode151_24_1_5^.md](md笔记\LeetCode151_24_1_5^.md)
|
||||
|
||||
[LeetCode209_23_12_10^.md](md笔记\LeetCode209_23_12_10^.md)
|
||||
|
||||
[LeetCode203_23_12_25^递归.md](md笔记\LeetCode203_23_12_25^递归.md)
|
||||
|
||||
[LeetCode206_23_12_28^递归.md](md笔记\LeetCode206_23_12_28^递归.md)
|
||||
|
||||
[LeetCode287_24_1_1^.md](md笔记\LeetCode287_24_1_1^.md)
|
||||
|
||||
[LeetCode202_24_1_2^.md](md笔记\LeetCode202_24_1_2^.md)
|
||||
|
||||
[LeetCode15_24_1_3^.md](md笔记\LeetCode15_24_1_3^.md)
|
||||
|
||||
[LeetCode2487_24_1_3^.md](md笔记\LeetCode2487_24_1_3^.md)
|
||||
|
||||
[LeetCode15_24_1_3^.md](md笔记\LeetCode15_24_1_3^.md)
|
||||
|
||||
[easyLeetCode28_24_1_5^](md笔记\LeetCode28_24_1_5^)
|
||||
|
||||
[LeetCode209_23_12_10^.md](md笔记\LeetCode209_23_12_10^.md)
|
||||
|
||||
[LeetCode225_24_1_8^.md](md笔记\LeetCode225_24_1_8^.md)
|
||||
|
||||
[LeetCode232_24_1_8^.md](md笔记\LeetCode232_24_1_8^.md)
|
||||
|
||||
[LeetCode239_24_1_9^.md](md笔记\LeetCode239_24_1_9^.md)
|
||||
|
||||
[LeetCode347_24_1_10^.md](md笔记\LeetCode347_24_1_10^.md)
|
||||
|
||||
[LeetCode94_2024_1_15^.md](2024-1月-15-22\LeetCode94_2024_1_15^.md)
|
||||
|
||||
[LeetCode102_2024_1_16^.md](2024-1月-15-22\LeetCode102_2024_1_16^.md)
|
||||
|
||||
[LeetCode144_2024_1_14^.md](2024-1月-15-22\LeetCode144_2024_1_14^.md)
|
||||
|
||||
[LeetCode145_2024_1_15^.md](2024-1月-15-22\LeetCode145_2024_1_15^.md)
|
||||
|
||||
[LeetCode199_2024_1_17^.md](2024-1月-15-22\LeetCode199_2024_1_17^.md)
|
||||
|
||||
[LeetCode101_24_1_21^.md](2024-1月-22-28\LeetCode101_24_1_21^.md)
|
||||
|
||||
[LeetCode110_24_1_22^.md](2024-1月-22-28\LeetCode110_24_1_22^.md)
|
||||
|
||||
[LeetCode257_24_1_22^.md](2024-1月-22-28\LeetCode257_24_1_22^.md)
|
||||
|
||||
[LeetCode105_24_1_26^.md](2024-1月-22-28\LeetCode105_24_1_26^.md)
|
||||
|
||||
[LeetCode106_24_1_26^.md](2024-1月-22-28\LeetCode106_24_1_26^.md)
|
||||
|
||||
[LeetCode222_24_1_22^.md](2024-1月-22-28\LeetCode222_24_1_22^.md)
|
||||
|
||||
[LeetCode77_24_2_1^.md](2024-1月29-2月4\LeetCode77_24_2_1^.md)
|
||||
|
||||
[LeetCode236_24_1_30^.md](2024-1月29-2月4\LeetCode236_24_1_30^.md)
|
||||
|
||||
[LeetCode216_24_2_2$.md](2024-1月29-2月4\LeetCode216_24_2_2$.md)
|
||||
|
||||
[LeetCode450_24_1_31^.md](2024-1月29-2月4\LeetCode450_24_1_31^.md)
|
||||
|
||||
[LeetCode501_24_1_30^.md](2024-1月29-2月4\LeetCode501_24_1_30^.md)
|
||||
|
||||
[LeetCode701_24_1_31$.md](2024-1月29-2月4\LeetCode701_24_1_31$.md)
|
||||
|
||||
[LeetCode40_24_2_7^.md](2024-2月6-2月18\LeetCode40_24_2_7^.md)
|
||||
|
||||
[LeetCode90_24_2_18^.md](2024-2月6-2月18\LeetCode90_24_2_18^.md)
|
||||
|
||||
[LeetCode131_24_2_10^.md](2024-2月6-2月18\LeetCode131_24_2_10^.md)
|
||||
|
||||
[LeetCode491_24_2_18^.md](2024-2月6-2月18\LeetCode491_24_2_18^.md)
|
||||
|
||||
[LeetCode46_24_2_19^.md](2024-2月19-2月24\LeetCode46_24_2_19^.md)[LeetCode47_24_2_19$](2024-2月19-2月24\LeetCode47_24_2_19$)
|
||||
|
||||
[LeetCode51n皇后_24_2_20^.md](2024-2月19-2月24\LeetCode51n皇后_24_2_20^.md)
|
||||
|
||||
[LeetCode332_24_2_20^difficult.md](2024-2月19-2月24\LeetCode332_24_2_20^difficult.md)
|
||||
|
||||
[LeetCode376_24_2_24^@.md](2024-2月26-3月1\LeetCode376_24_2_24^@.md)
|
||||
|
||||
[LeetCode53_24_2_25^@.md](2024-2月26-3月1\LeetCode53_24_2_25^@.md)
|
||||
|
||||
[LeetCode122_24_2_25@.md](2024-2月26-3月1\LeetCode122_24_2_25@.md)
|
||||
|
||||
[LeetCode55_24_2_26^.md](2024-2月26-3月1\LeetCode55_24_2_26^.md)
|
||||
|
||||
[LeetCode45_24_2_27^.md](2024-2月26-3月1\LeetCode45_24_2_27^.md)
|
||||
|
||||
[LeetCode134_24_2_27^.md](2024-2月26-3月1\LeetCode134_24_2_27^.md)
|
||||
|
||||
[LeetCode1005_24_2_27^.md](2024-2月26-3月1\LeetCode1005_24_2_27^.md)
|
||||
|
||||
[LeetCode860_24_2_28^.md](2024-2月26-3月1\LeetCode860_24_2_28^.md)
|
||||
|
||||
[LeetCode135_24_2_28^.md](2024-2月26-3月1\LeetCode135_24_2_28^.md)
|
||||
|
||||
[LeetCode406_24_2_29^.md](2024-2月26-3月1\LeetCode406_24_2_29^.md)
|
||||
|
||||
[LeetCode435_24_3_4^.md](2024-3月4-3月9\LeetCode435_24_3_4^.md)
|
||||
|
||||
[LeetCode452_24_3_4^.md](2024-3月4-3月9\LeetCode452_24_3_4^.md)
|
||||
|
||||
[LeetCode763_24_3_5^.md](2024-3月4-3月9\LeetCode763_24_3_5^.md)
|
||||
|
||||
[LeetCode738_24_3_6^.md](2024-3月4-3月9\LeetCode738_24_3_6^.md)
|
||||
|
||||
[LeetCode968_24_3_6^.md](2024-3月4-3月9\LeetCode968_24_3_6^.md)
|
||||
|
||||
[LeetCode509_24_3_8^.md](2024-3月4-3月9\LeetCode509_24_3_8^.md)
|
||||
|
||||
[LeetCode96_24_3_11^.md](2024-3月11-3月16\LeetCode96_24_3_11^.md)
|
||||
|
||||
[LeetCode343_24_3_11^.md](2024-3月11-3月16\LeetCode343_24_3_11^.md)
|
||||
|
||||
[LeetCode416_24_3_14^.md](2024-3月11-3月16\LeetCode416_24_3_14^.md)
|
||||
|
||||
[0-1背包问题_24_3_14^.md](2024-3月11-3月16\0-1背包问题_24_3_14^.md)
|
||||
|
||||
[LCR001_24_3_21^.md](2024-3月18-3月23\LCR001_24_3_21^.md)
|
||||
|
||||
[LCR090_24_3_21^.md](2024-3月18-3月23\LCR090_24_3_21^.md)
|
||||
|
||||
[LeetCode5_24_3_22^.md](2024-3月18-3月23\LeetCode5_24_3_22^.md)
|
||||
|
||||
[LeetCode647_24_3_22^.md](2024-3月18-3月23\LeetCode647_24_3_22^.md)
|
||||
|
||||
[LeetCode713_24_3_18^.md](2024-3月18-3月23\LeetCode713_24_3_18^.md)
|
||||
|
||||
[LeetCode1049_24_3_18^.md](2024-3月18-3月23\LeetCode1049_24_3_18^.md)
|
||||
|
||||
[最大连续子串问题_24_3_29.md](2024-3月25-4月1\最大连续子串问题_24_3_29.md)
|
||||
|
||||
[携程_24_3_28笔试.md](..\笔试或面试\携程_24_3_28笔试.md)
|
||||
[LeetCode53_24_2_25^@.md](..\2024-2月26-3月1\LeetCode53_24_2_25^@.md)
|
||||
|
||||
[LeetCode209_24_3_29^前缀和解法.md](2024-3月25-4月1\LeetCode209_24_3_29^前缀和解法.md):
|
||||
|
||||
[AbsoluteBeibao_24_3_26^.md](2024-3月25-4月1\AbsoluteBeibao_24_3_26^.md)
|
||||
|
||||
[LeetCode279_24_3_28^.md](2024-3月25-4月1\LeetCode279_24_3_28^.md)
|
||||
|
||||
[LeetCode322_24_3_27^.md](2024-3月25-4月1\LeetCode322_24_3_27^.md)
|
||||
|
||||
[LeetCode377_24_3_27^.md](2024-3月25-4月1\LeetCode377_24_3_27^.md)
|
||||
|
||||
[LeetCode474_24_3_26^.md](2024-3月25-4月1\LeetCode474_24_3_26^.md)
|
||||
|
||||
[LeetCode494_24_3_24^.md](2024-3月25-4月1\LeetCode494_24_3_24^.md)
|
||||
|
||||
[LeetCode2602_24_3_29^.md](2024-3月25-4月1\LeetCode2602_24_3_29^.md)
|
||||
|
||||
[meituan240309_24_3_24^.md](2024-3月25-4月1\meituan240309_24_3_24^.md)
|
||||
|
||||
[PaLouTi24_3_27.md](2024-3月25-4月1\PaLouTi24_3_27.md)
|
||||
|
||||
[LeetCode11_24_4_3^.md](2024-4月2-4月7\LeetCode11_24_4_3^.md)
|
||||
|
||||
[LeetCode21_24_4_4^.md](2024-4月2-4月7\LeetCode21_24_4_4^.md)
|
||||
|
||||
[LeetCode22_24_4_4^.md](2024-4月2-4月7\LeetCode22_24_4_4^.md)
|
||||
|
||||
[LeetCode23_24_4_5@.md](2024-4月2-4月7\LeetCode23_24_4_5@.md)
|
||||
|
||||
[LeetCode31_24_4_5^.md](2024-4月2-4月7\LeetCode31_24_4_5^.md)
|
||||
|
||||
[LeetCode32_24_4_6^.md](2024-4月2-4月7\LeetCode32_24_4_6^.md)
|
||||
|
||||
[LeetCode121_24_4_3^.md](2024-4月2-4月7\LeetCode121_24_4_3^.md)
|
||||
|
||||
[LeetCode337_24_4_3^.md](2024-4月2-4月7\LeetCode337_24_4_3^.md)
|
||||
|
||||
[LeetCode42_24_4_8^.md](2024-4月8-4月13\LeetCode42_24_4_8^.md)
|
||||
|
||||
[LeetCode84_24_4_11^.md](2024-4月8-4月13\LeetCode84_24_4_11^.md)
|
||||
|
||||
[LeetCode496_24_4_9^.md](2024-4月8-4月13\LeetCode496_24_4_9^.md)
|
||||
|
||||
[LeetCode503_24_4_11^.md](2024-4月8-4月13\LeetCode503_24_4_11^.md)
|
||||
|
||||
[LeetCode739_24_4_8^.md](2024-4月8-4月13\LeetCode739_24_4_8^.md)
|
||||
|
||||
[LeetCode33_24_4_15^.md](2024-4月15-4月20\LeetCode33_24_4_15^.md)
|
||||
|
||||
[LeetCode48_24_4_15@.md](2024-4月15-4月20\LeetCode48_24_4_15@.md)
|
||||
|
||||
[LeetCode69_23_12_3^.md](2024-4月15-4月20\LeetCode69_23_12_3^.md)
|
||||
|
||||
[LeetCode72_24_4_16^.md](2024-4月15-4月20\LeetCode72_24_4_16^.md)
|
||||
|
||||
[LeetCode75_24_4_17^.md](2024-4月15-4月20\LeetCode75_24_4_17^.md)
|
||||
|
||||
[LeetCode309_24_4_16^](2024-4月15-4月20\LeetCode309_24_4_16^)
|
||||
|
||||
[排序算法.md](2024-4月15-4月20\排序算法.md)
|
||||
|
||||
[LeetCode23_24_4_5@.md](2024-4月2-4月7\LeetCode23_24_4_5@.md)
|
||||
|
||||
[LeetCode32_24_4_6^.md](2024-4月2-4月7\LeetCode32_24_4_6^.md)
|
||||
|
||||
[LeetCode337_24_4_3^.md](2024-4月2-4月7\LeetCode337_24_4_3^.md)
|
||||
|
||||
[LeetCode84_24_4_11^.md](2024-4月8-4月13\LeetCode84_24_4_11^.md)
|
||||
|
||||
[LeetCode115_24_4_26^.md](2024-4月22-4月27\LeetCode115_24_4_26^.md)
|
||||
|
||||
[LeetCode300_24_4_22^.md](2024-4月22-4月27\LeetCode300_24_4_22^.md)
|
||||
|
||||
[LeetCode392_24_4_25@.md](2024-4月22-4月27\LeetCode392_24_4_25@.md)
|
||||
|
||||
[LeetCode674_24_4_22@.md](2024-4月22-4月27\LeetCode674_24_4_22@.md)
|
||||
|
||||
[LeetCode718_24_4_23^.md](2024-4月22-4月27\LeetCode718_24_4_23^.md)
|
||||
|
||||
[LeetCode1035_24_4_25^.md](2024-4月22-4月27\LeetCode1035_24_4_25^.md)
|
||||
|
||||
[LeetCode1143_24_4_24^.md](2024-4月22-4月27\LeetCode1143_24_4_24^.md)
|
||||
|
||||
[LeetCode583_24_5_2.md](2024-5月1-5月5\LeetCode583_24_5_2.md)
|
||||
|
||||
[LeetCode647^.md](2024-5月1-5月5\LeetCode647^.md)
|
||||
|
@ -0,0 +1,94 @@
|
||||
package cn.whaifree.tech.thread;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.Semaphore;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/5/12 19:10
|
||||
* @注释
|
||||
*/
|
||||
public class Alternate_printing {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Semaphore s0 = new Semaphore(1);
|
||||
Semaphore s1 = new Semaphore(0);
|
||||
|
||||
new Thread(new FutureTask<>(new Callable<>() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
for (int i = 0; i < 50; i++) {
|
||||
s0.acquire();
|
||||
System.out.println("a");
|
||||
s1.release();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
})).start();
|
||||
|
||||
|
||||
new Thread(new FutureTask<>(new Callable<>() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
for (int i = 0; i < 50; i++) {
|
||||
s1.acquire();
|
||||
System.out.println("b");
|
||||
s0.release();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
})).start();
|
||||
}
|
||||
}
|
||||
|
||||
class Alternate_printing_2 {
|
||||
|
||||
static Object o = new Object();
|
||||
static volatile int num = 0;
|
||||
|
||||
public static void main(String[] args) throws FileNotFoundException {
|
||||
FileInputStream fileInputStream = new FileInputStream("file");
|
||||
// BufferedInputStream 装饰器类
|
||||
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
|
||||
|
||||
new Thread(new FutureTask<>(new Callable<>() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
while (num < 100) {
|
||||
synchronized (o) {
|
||||
while (num % 2 == 0) {
|
||||
o.wait();
|
||||
}
|
||||
System.out.println("a" + num);
|
||||
num++;
|
||||
o.notifyAll();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
})).start();
|
||||
|
||||
|
||||
new Thread(new FutureTask<>(new Callable<>() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
while (num < 100) {
|
||||
synchronized (o) {
|
||||
while (num % 2 == 1) { // 自旋
|
||||
o.wait();
|
||||
}
|
||||
System.out.println("b" + num);
|
||||
num++;
|
||||
o.notifyAll();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
})).start();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user