diff --git a/src/main/java/cn/whaifree/designPattern/AbstractFactoryPattern.java b/src/main/java/cn/whaifree/designPattern/AbstractFactoryPattern.java new file mode 100644 index 0000000..8186dd6 --- /dev/null +++ b/src/main/java/cn/whaifree/designPattern/AbstractFactoryPattern.java @@ -0,0 +1,102 @@ +package cn.whaifree.designPattern; + +public class AbstractFactoryPattern { + + // https://kamacoder.com/problempage.php?pid=1077 + public static void main(String[] args) { + ModernFactory factory = new ModernFactory(); + Sofa sofa = factory.generateSofa(); + sofa.proSofa(); + Chair chair = factory.generateChair(); + chair.proChair(); + ClassicFactory classicFactory = new ClassicFactory(); + Sofa sofa1 = classicFactory.generateSofa(); + sofa1.proSofa(); + Chair chair1 = classicFactory.generateChair(); + chair1.proChair(); + } +} + + +interface Sofa{ + void proSofa(); +} + +interface Chair{ + void proChair(); +} + +class ClassSofa implements Sofa{ + + @Override + public void proSofa() { + System.out.println("class sofa"); + } +} + +class ModernSofa implements Sofa{ + + @Override + public void proSofa() { + System.out.println("modern sofa"); + } +} + +class ClassChair implements Chair{ + + @Override + public void proChair() { + System.out.println("class chair"); + } +} + +class ModernChair implements Chair{ + + @Override + public void proChair() { + System.out.println("modern chair"); + } +} + + + + +interface AbstractFactory{ + Sofa generateSofa(); + Chair generateChair(); +} + +class ModernFactory implements AbstractFactory { + + @Override + public Sofa generateSofa() { + return new ModernSofa(); + } + + @Override + public Chair generateChair() { + + return new ModernChair(); + } + +} + +class ClassicFactory implements AbstractFactory { + + @Override + public Sofa generateSofa() { + + return new ClassSofa(); + } + + @Override + public Chair generateChair() { + + return new ClassChair(); + } + +} + + + + diff --git a/src/main/java/cn/whaifree/leetCode/String/LeetCode415.java b/src/main/java/cn/whaifree/leetCode/String/LeetCode415.java new file mode 100644 index 0000000..4dee1ae --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/String/LeetCode415.java @@ -0,0 +1,70 @@ +package cn.whaifree.leetCode.String; + +import org.junit.Test; + +public class LeetCode415 { + + @Test + public void main() { + System.out.println(new Solution().addStrings("111", "59999")); + } + + + class Solution { + public String addStrings(String num1, String num2) { + char[] c1 = num1.toCharArray(); + char[] c2 = num2.toCharArray(); + int index1 = c1.length - 1; + int index2 = c2.length - 1; + StringBuilder stringBuilder = new StringBuilder(); + boolean retail = false; + while (index1 >= 0 && index2 >= 0) { + int char1 = c1[index1] - '0'; + int char2 = c2[index2] - '0'; + int tmpAns = char1 + char2; + if (retail) { + tmpAns += 1; + retail = false; + } + if (tmpAns >= 10) { + tmpAns %= 10; + retail = true; + } + stringBuilder.append(tmpAns); + index2--; + index1--; + } + + while (index1 >= 0) { + int tmpAns = c1[index1--] - '0'; + if (retail) { + tmpAns += 1; + retail = false; + } + if (tmpAns >= 10) { + tmpAns %= 10; + retail = true; + } + stringBuilder.append( tmpAns); + } + + while (index2 >= 0) { + int tmpAns = c2[index2--] - '0'; + if (retail) { + tmpAns += 1; + retail = false; + } + if (tmpAns >= 10) { + tmpAns %= 10; + retail = true; + } + stringBuilder.append(tmpAns); + } + + if (retail) { + stringBuilder.append(1); + } + return stringBuilder.reverse().toString(); + } + } +} \ No newline at end of file diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode93.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode93.java new file mode 100644 index 0000000..8553211 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode93.java @@ -0,0 +1,63 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +public class LeetCode93 { + + @Test + public void test() { + System.out.println(new Solution().restoreIpAddresses("25525511135")); + } + + + class Solution { + + List path = new ArrayList<>(); + List ans = new ArrayList<>(); + public List restoreIpAddresses(String s) { + backTracking(s, 0, 0); + return ans; + } + + public void backTracking(String s, int start,int number) { + if (number > 4 ) { + return; + } + + if (start >= s.length() && path.size() == 4) { + StringBuilder stringBuilder = new StringBuilder(); + for (String string : path) { + stringBuilder.append(string).append("."); + } + stringBuilder.deleteCharAt(stringBuilder.length() - 1); + ans.add(stringBuilder.toString()); + } + + for (int i = start; i < s.length(); i++) { + if (isValid(s, start, i + 1)) { + path.add(s.substring(start, i + 1)); + backTracking(s, i + 1, number + 1); + path.remove(path.size() - 1); + } + } + + } + + public boolean isValid(String s, int start, int end) { + String substring = s.substring(start, end); + if (substring.length() > 3) { + return false; + } + + + if (substring.length() >= 2 && substring.startsWith("0")) { + return false; + } + int integer = Integer.parseInt(substring); + return integer >= 0 && integer <= 255; + } + } +} diff --git a/src/main/java/cn/whaifree/test/ThreadDemo1.java b/src/main/java/cn/whaifree/test/ThreadDemo1.java index 3a7ce81..fccd2d0 100644 --- a/src/main/java/cn/whaifree/test/ThreadDemo1.java +++ b/src/main/java/cn/whaifree/test/ThreadDemo1.java @@ -6,7 +6,9 @@ import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantLock; +import java.util.function.Function; import java.util.function.Supplier; +import java.util.stream.Collectors; public class ThreadDemo1 { @@ -191,10 +193,46 @@ class p2{ } } +class dMyException extends Exception { -class mockException{ + static ConcurrentHashMap map = new ConcurrentHashMap<>(); public static void main(String[] args) { + new Thread(() -> { + for (int i = 0; i <100 ; i++) { + if (map.containsKey("key")) { + map.remove("key"); + } + + } + }).start(); + + new Thread(() -> { + for (int i = 0; i < 100; i++) { + if (!map.containsKey("key")) { + map.put("key", 1); + } + } + }).start(); + } + +} +class mockException{ + + public static void main(String[] args) throws ExecutionException, InterruptedException { + + + + + List> futures = new ArrayList<>(); + CompletableFuture future = CompletableFuture.supplyAsync( + () -> 1 + , Executors.newFixedThreadPool(10)); + futures.add(future); + for (Future integerFuture : futures) { + System.out.println(integerFuture.get()); + } + ThreadLocal objectThreadLocal = new InheritableThreadLocal<>();