diff --git a/src/main/java/cn/whaifree/interview/xiaotiancai/P1.java b/src/main/java/cn/whaifree/interview/xiaotiancai/P1.java new file mode 100644 index 0000000..a516452 --- /dev/null +++ b/src/main/java/cn/whaifree/interview/xiaotiancai/P1.java @@ -0,0 +1,35 @@ +package cn.whaifree.interview.xiaotiancai; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/17 0:00 + * @注释 + */ +public class P1 { + + static { + System.out.println(1); + } + public static void main(String[] args) { + System.out.println(1/0); + System.out.println(3); + } + + static { + System.out.println(2); + } + + class par{ + void test() { + System.out.println(1/0); + } + } + + class sun extends par { + public void test() { + + } + + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Array/LeetCode238.java b/src/main/java/cn/whaifree/leetCode/Array/LeetCode238.java new file mode 100644 index 0000000..1314c6e --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Array/LeetCode238.java @@ -0,0 +1,77 @@ +package cn.whaifree.leetCode.Array; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/17 13:58 + * @注释 + */ +public class LeetCode238 { + @Test + public void test() { + int[] nums = {1, 2, 3, 4}; + int[] ints = productExceptSelf(nums); + for (int anInt : ints) { + System.out.println(anInt); + } + } + + class Solution { + /** + * [-1,1,0,-3,3] + * -1 -1 0 0 0 + * 0 0 0 -9 3 + * + * @param nums + * @return + */ + public int[] productExceptSelf(int[] nums) { + int[] preMul = new int[nums.length]; + preMul[0] = nums[0]; + for (int i = 1; i < nums.length; i++) { + preMul[i] = preMul[i - 1] * nums[i]; + } + + int[] afterMul = new int[nums.length]; + afterMul[nums.length - 1] = nums[nums.length - 1]; + for (int i = nums.length - 2; i >= 0; i--) { + afterMul[i] = afterMul[i + 1] * nums[i]; + } + + for (int i = 0; i < nums.length; i++) { + int pre = (i > 0) ? preMul[i - 1] : 1; + int after = i < nums.length - 1 ? afterMul[i + 1] : 1; + nums[i] = pre * after; + } + + return nums; + } + } + + //维护两个变量,beforeSum表示前缀和,afterSum表示后缀和 + // 两个指针不断缩小会相交 + public int[] productExceptSelf(int[] nums) { + int n = nums.length; + int[] ans = new int[n]; + Arrays.fill(ans, 1); + int beforeSum = 1; + int afterSum = 1; + + int left = 0; + int right = nums.length - 1; + while (left < nums.length) { + ans[left] *= beforeSum; + ans[right] *= afterSum; + beforeSum *= nums[left]; + afterSum *= nums[right]; + left++; + right--; + } + + return ans; + } +} diff --git a/src/main/java/cn/whaifree/leetCode/String/LeetCode13.java b/src/main/java/cn/whaifree/leetCode/String/LeetCode13.java new file mode 100644 index 0000000..62f43b6 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/String/LeetCode13.java @@ -0,0 +1,123 @@ +package cn.whaifree.leetCode.String; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/18 0:11 + * @注释 + */ +public class LeetCode13 { + + @Test + public void test() + { + Solution1 solution = new Solution1(); + int i = solution.romanToInt("MCMXCIV"); + System.out.println(i); + } + + class Solution { + public int romanToInt(String s) { + char[] charArray = s.toCharArray(); + int res = 0; + for (int i = 0; i < charArray.length; i++) { + if (i < charArray.length - 1 && charArray[i] == 'I' && charArray[i + 1] == 'V') { + res += 4; + i++; + } + else if (i < charArray.length - 1 && charArray[i] == 'I' && charArray[i + 1] == 'X') { + res += 9; + i++; + } + else if (i < charArray.length - 1 && charArray[i] == 'X' && charArray[i + 1] == 'L') { + res += 40; + i++; + } + else if (i < charArray.length - 1 && charArray[i] == 'X' && charArray[i + 1] == 'C') { + res += 90; + i++; + } + else if (i < charArray.length - 1 && charArray[i] == 'C' && charArray[i + 1] == 'D') { + res += 400; + i++; + } + else if (i < charArray.length - 1 && charArray[i] == 'C' && charArray[i + 1] == 'M') { + res += 900; + i++; + } + else if (charArray[i] == 'I') { + res += 1; + } + else if (charArray[i] == 'V') { + res += 5; + } + else if (charArray[i] == 'X') { + res += 10; + } + else if (charArray[i] == 'L') { + res += 50; + } + else if (charArray[i] == 'C') { + res += 100; + } + else if (charArray[i] == 'D') { + res += 500; + } + else if (charArray[i] == 'M') { + res += 1000; + } + } + return res; + } + } + + class Solution1 { + + + /** + * 当前位置的元素比下个位置的元素小,就减去当前值,否则加上当前值 + * 输入: s = "MCMXCIV" + * 输出: 1994 + * 解释: M = 1000, CM = 900, XC = 90, IV = 4. + * @param s + * @return + */ + public int romanToInt(String s) { + int res = 0; + int preV = getV(s.charAt(0)); + for (int i = 1; i < s.length(); i++) { + int nums = getV(s.charAt(i)); + if (preV < nums) { + res -= preV; + }else { + res += preV; + } + preV = nums; + } + res += preV; + return res; + } + + public int getV(char c) { + switch (c) { + case 'I': + return 1; + case 'V': + return 5; + case 'X': + return 10; + case 'L': + return 50; + case 'C': + return 100; + case 'D': + return 500; + case 'M': + return 1000; + } + return 0; + } + } +} diff --git a/src/main/java/cn/whaifree/test/ThreadDemo1.java b/src/main/java/cn/whaifree/test/ThreadDemo1.java index fccd2d0..0df1a92 100644 --- a/src/main/java/cn/whaifree/test/ThreadDemo1.java +++ b/src/main/java/cn/whaifree/test/ThreadDemo1.java @@ -1,14 +1,10 @@ package cn.whaifree.test; -import javax.swing.plaf.synth.SynthOptionPaneUI; -import java.lang.reflect.Proxy; 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 { @@ -67,6 +63,7 @@ public class ThreadDemo1 { }).start(); } + countDownLatch.await(); new Thread(() -> System.out.println("上面的9个执行完了,轮到我了 wait complete!")).start();