diff --git a/src/main/java/cn/whaifree/interview/qz/huachen/P1.java b/src/main/java/cn/whaifree/interview/qz/huachen/P1.java new file mode 100644 index 0000000..ec14f8f --- /dev/null +++ b/src/main/java/cn/whaifree/interview/qz/huachen/P1.java @@ -0,0 +1,10 @@ +package cn.whaifree.interview.qz.huachen; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/19 18:55 + * @注释 + */ +public class P1 { +} diff --git a/src/main/java/cn/whaifree/interview/qz/zzyc/p1.java b/src/main/java/cn/whaifree/interview/qz/zzyc/p1.java new file mode 100644 index 0000000..1d0e83f --- /dev/null +++ b/src/main/java/cn/whaifree/interview/qz/zzyc/p1.java @@ -0,0 +1,137 @@ +package cn.whaifree.interview.qz.zzyc; + +import java.util.*; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/20 0:35 + * @注释 + */ +public class p1 { + + public static void main(String[] args) { + + List list = Arrays.asList(0, 11, 12, 13,1); + System.out.println(new p1().isStraight(new ArrayList<>(list))); + + } + + /** + * + * 1. 排序 + * + * + * 给定的5张牌是否是顺子 + * @param nums int整型ArrayList 扑克牌对应的数字集合 + * @return bool布尔型 + */ + public boolean isStraight (ArrayList nums) { + // write code here + + Collections.sort(nums); + + + if (inEdge(nums)) { // 如果出现1 10 11 12 13 这种情况 + return true; + } + + int joker = -1; + for (int i = 0; i < nums.size() - 1; i++) { + if (nums.get(i) == 0) { + joker++; + } else if (nums.get(i).equals(nums.get(i + 1))) { + return false; // 排序后出现重复,不可能是顺子 + } + } + + // 比如 5 4 3 2 1 最大的减去最小在范围内 + return nums.get(4) - nums.get(joker + 1) < 5; + } + + + static ArrayList> o = new ArrayList<>(); + static { + o.add(new ArrayList<>(Arrays.asList(0, 11, 12, 13, 1))); + o.add(new ArrayList<>(Arrays.asList(0, 10, 12, 13, 1))); + o.add(new ArrayList<>(Arrays.asList(0, 10, 11, 13, 1))); + o.add(new ArrayList<>(Arrays.asList(0, 10, 11, 12, 1))); + o.add(new ArrayList<>(Arrays.asList(0, 10, 11, 12, 13))); + o.add(new ArrayList<>(Arrays.asList(1, 10, 11, 12, 13))); + } + public static boolean inEdge(ArrayList list) { + for (ArrayList integers : o) { + if (integers.equals(list)) { + return true; + } + } + return false; + } + + class Solution { + public boolean checkDynasty(int[] places) { + Set repeat = new HashSet<>(); + int max = 0, min = 14; + for(int place : places) { + if(place == 0) continue; // 跳过未知朝代 + max = Math.max(max, place); // 最大编号朝代 + min = Math.min(min, place); // 最小编号朝代 + if(repeat.contains(place)) { + return false; // 若有重复,提前返回 false + } + repeat.add(place); // 添加此朝代至 Set + } + return max - min < 5; // 最大编号朝代 - 最小编号朝代 < 5 则连续 + } + } + +} + + +class P{ + + public static void main(String[] args) { + List list = Arrays.asList(1, 4, 6, 6, 11); + + System.out.println(prize(new ArrayList<>(list), 12)); + } + + + /** + * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 + * + * + * @param res int整型ArrayList + * @param target int整型 + * @return int整型 + */ + public static int prize (ArrayList res, int target) { + // write code here + +// for (int i = 0; i < res.size(); i++) { +// if (res.get(i) >= target) { +// return i; +// } +// } + + + int left = 0; + int right = res.size() - 1; + while (left <= right) { + int mid = (left + right) / 2; + if (res.get(mid) > target) { + right = mid - 1; + } else if (res.get(mid) == target) { + return mid + 1; + }else { + left = mid + 1; + } + } + if (left < res.size() && res.get(left) > target) { + return left + 1; + } + + + return -1; + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Array/LeetCode167.java b/src/main/java/cn/whaifree/leetCode/Array/LeetCode167.java new file mode 100644 index 0000000..7bd91cd --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Array/LeetCode167.java @@ -0,0 +1,68 @@ +package cn.whaifree.leetCode.Array; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/19 23:02 + * @注释 + */ +public class LeetCode167 { + + @Test + public void test() { + + Solution1 solution = new Solution1(); + int[] ints = solution.twoSum(new int[]{2,7,11,15}, 9); + System.out.println(ints[0] + " " + ints[1]); + } + + class Solution { + public int[] twoSum(int[] numbers, int target) { + + for (int i = 0; i < numbers.length; i++) { + int tar = target - numbers[i]; + int j = binarySearch(numbers, tar); + if (j != -1 && i != j) { + return i < j ? new int[]{i + 1, j + 1} : new int[]{j + 1, i + 1}; + } + } + return null; + } + + public int binarySearch(int[] nums, int target) { + int left = 0; + int right = nums.length - 1; + while (left <= right) { + int mid = (left + right) / 2; + if (nums[mid] == target) { + return mid; + } else if (nums[mid] < target) { + left = mid + 1; + } else { + right = mid - 1; + } + } + return -1; + } + } + + class Solution1 { + public int[] twoSum(int[] numbers, int target) { + int left = 0; + int right = numbers.length - 1; + while (left < right) { + int sum = numbers[left] + numbers[right]; + if (sum == target) { + return new int[]{left + 1, right + 1}; + } else if (sum < target) { + left++; + }else { + right--; + } + } + return null; + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode139.java b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode139.java index 54dcd46..2503cec 100644 --- a/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode139.java +++ b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode139.java @@ -1,5 +1,7 @@ package cn.whaifree.leetCode.Dynamic; +import java.util.List; + /** * @version 1.0 * @Author whai文海 @@ -10,12 +12,19 @@ public class LeetCode139 { class Solution { -// public boolean wordBreak(String s, List wordDict) { -// // wordDict放入s的背包 -// char[] chars = s.toCharArray(); -// -// -// } + /** + * + * @param s + * @param wordDict + * @return + */ + public boolean wordBreak(String s, List wordDict) { + for (int i = 0; i < s.length(); i++) { + + } + + return true; + } } diff --git a/src/main/java/cn/whaifree/leetCode/Hash/LeetCode128.java b/src/main/java/cn/whaifree/leetCode/Hash/LeetCode128.java new file mode 100644 index 0000000..7f021c4 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Hash/LeetCode128.java @@ -0,0 +1,45 @@ +package cn.whaifree.leetCode.Hash; + +import org.junit.Test; + +import java.util.HashSet; +import java.util.Set; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/21 13:53 + * @注释 + */ +public class LeetCode128 { + + @Test + public void test() { + int[] nums = {4, 1, 3, 2}; + System.out.println(longestConsecutive(nums)); + } + + public int longestConsecutive(int[] nums) { + Set set = new HashSet<>(); + for (int num : nums) { + set.add(num); + } + + + int longest = 0; + // ON + for (int num : set) { + + // 这里是关键,说明这个元素i是某一组排序的开头,不存在i-1的元素 + if (!set.contains(num-1)){ + int len = 1; + while (set.contains(++num)) { + len++; + } + longest = Math.max(longest, len); + } + + } + return longest; + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Hash/LeetCode205.java b/src/main/java/cn/whaifree/leetCode/Hash/LeetCode205.java new file mode 100644 index 0000000..4e4c298 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Hash/LeetCode205.java @@ -0,0 +1,68 @@ +package cn.whaifree.leetCode.Hash; + +import org.junit.Test; + +import java.util.HashMap; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/21 13:32 + * @注释 + */ +public class LeetCode205 { + @Test + public void test() { + String s = "badc"; + String t = "baba"; + System.out.println(new Solution().isIsomorphic(s, t)); + } + + class Solution { + public boolean isIsomorphic(String s, String t) { + return is(s, t) && is(t, s); + } + + public boolean is(String s, String t) { + HashMap map = new HashMap<>(); + for (int i = 0; i < s.length(); i++) { + char key = s.charAt(i); + if (!map.containsKey(key)) { + map.put(key, t.charAt(i)); + } else if (map.get(key) != t.charAt(i)) { + return false; + } + } + return true; + + } + + + } + class Solution1 { + /** + * 每个字符第一次出现的位置都一样,就能保证同构成 + * abc c和d第一次出现的 位置为1和2,不一样会false + * cdd + * + * indexOf时间复杂度oN + * @param s + * @param t + * @return + */ + public boolean isIsomorphic(String s, String t) { + if (s.length() != t.length()) { + return false; + } + + for (int i = 0; i < s.length(); i++) { + char s1 = s.charAt(i); + char t1 = t.charAt(i); + if (s.indexOf(s1) != t.indexOf(t1)) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/LinkedList/LeetCode92.java b/src/main/java/cn/whaifree/leetCode/LinkedList/LeetCode92.java new file mode 100644 index 0000000..1ccda67 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/LinkedList/LeetCode92.java @@ -0,0 +1,64 @@ +package cn.whaifree.leetCode.LinkedList; + +import cn.whaifree.leetCode.model.ListNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/21 14:11 + * @注释 + */ +public class LeetCode92 { + + @Test + public void test() { + new Solution().reverseBetween(ListNode.listNodeFromArray(new int[]{1, 2, 3, 4, 5} + ), 1, 5).printList(); + } + + class Solution { + public ListNode reverseBetween(ListNode head, int left, int right) { + if (left == right) { + return head; + } + ListNode pre = new ListNode(-1, head); + ListNode tmpLeft = pre; + ListNode tmpRight = pre; + for (int i = 0; i < left - 1; i++) { + tmpLeft = tmpLeft.next; + tmpRight = tmpRight.next; + } + for (int i = 0; i < right - left + 2; i++) { + tmpRight = tmpRight.next; + } + + ListNode tmpNext = tmpLeft.next; + tmpLeft.next = reverse(null, tmpNext, right - left + 1); + + tmpNext.next = tmpRight; + + return pre.next; + } + + + public ListNode reverse(ListNode pre, ListNode after, int k) { + if (k == 0) { + return pre; + } + if (after == null) { + return pre; + } + ListNode tmp = after.next; + after.next = pre; + return reverse(after, tmp, k - 1); + } + + } + + @Test + public void test2() { + ListNode pre = ListNode.listNodeFromArray(new int[]{1, 2, 3}); + new Solution().reverse(null, pre, 3).printList(); + } +} diff --git a/src/main/java/cn/whaifree/leetCode/String/LeetCode468.java b/src/main/java/cn/whaifree/leetCode/String/LeetCode468.java new file mode 100644 index 0000000..3a85cc7 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/String/LeetCode468.java @@ -0,0 +1,82 @@ +package cn.whaifree.leetCode.String; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/19 22:08 + * @注释 + */ +public class LeetCode468 { + + @Test + public void test() { + + System.out.println(5 & 0xFF); + + Solution solution = new Solution(); + System.out.println(solution.validIPAddress("20EE:Fb8:85a3:0:0:8A2E:0370:7334:12")); + } + + class Solution { + public String validIPAddress(String queryIP) { + boolean ipv6 = queryIP.contains(":"); + if (ipv6 && isIpv6(queryIP)) { + return "IPv6"; + } else if (!ipv6 && isIpv4(queryIP)){ + return "IPv4"; + } + return "Neither"; + } + + + public boolean isIpv6(String s) { + String[] split = s.split(":", -1); + if (split.length != 8) { + return false; + } + for (String item : split) { + if (item.length() > 4 || item.isEmpty()) { + return false; + } + + try { + int i = Integer.parseInt(item, 16);// 16进制 + + } catch (Exception e) { + return false; + } + } + return true; + } + + public boolean isIpv4(String s) { + String[] split = s.split("\\."); + if (split.length != 4) { + return false; + } + for (String item : split) { + if (item.length() > 3 || item.isEmpty()) { + return false; + } + try { + int i = Integer.parseInt(item); + if (i == 0 && item.length() != 1) { + return false; + } + if (item.startsWith("0") && i != 0) { // 00.0.0.0. + return false; + } + if (i < 0 || i > 255) { + return false; + } + } catch (Exception e) { + return false; + } + + } + return true; + } + } +} diff --git a/src/main/java/cn/whaifree/test/StampedLockDemo.java b/src/main/java/cn/whaifree/test/StampedLockDemo.java new file mode 100644 index 0000000..9e7fcb9 --- /dev/null +++ b/src/main/java/cn/whaifree/test/StampedLockDemo.java @@ -0,0 +1,59 @@ +package cn.whaifree.test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/21 16:24 + * @注释 + */ +import java.util.concurrent.locks.StampedLock; + +public class StampedLockDemo { + private int count = 0; + private final StampedLock lock = new StampedLock(); + + public void increment() { + long stamp = lock.writeLock(); // 获取写锁 + try { + count++; + } finally { + lock.unlockWrite(stamp); // 释放写锁 + } + } + + public int getCount() { + long stamp = lock.tryOptimisticRead(); // 获取乐观读锁 + int c = count; + if (!lock.validate(stamp)) { // 检查乐观读锁是否有效, 如果乐观锁无效,使用悲观锁 + stamp = lock.readLock(); // 获取悲观读锁 + try { + c = count; + } finally { + lock.unlockRead(stamp); // 释放悲观读锁 + } + } + return c; + } + + public static void main(String[] args) { + StampedLockDemo demo = new StampedLockDemo(); + Thread writer = new Thread(() -> { + for (int i = 0; i < 1000; i++) { + demo.increment(); + } + }); + Thread reader = new Thread(() -> { + for (int i = 0; i < 1000; i++) { + System.out.println(demo.getCount()); + } + }); + writer.start(); + reader.start(); + try { + writer.join(); + reader.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/cn/whaifree/test/TestCacheThreadPool.java b/src/main/java/cn/whaifree/test/TestCacheThreadPool.java index f272958..b1f7c70 100644 --- a/src/main/java/cn/whaifree/test/TestCacheThreadPool.java +++ b/src/main/java/cn/whaifree/test/TestCacheThreadPool.java @@ -5,9 +5,7 @@ import org.junit.Test; import java.io.ByteArrayInputStream; import java.io.IOException; import java.nio.charset.Charset; -import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; @@ -135,6 +133,7 @@ public class TestCacheThreadPool { private String message = "等待消息"; public static void main(String[] args) { + MultiConditionDemo demo = new MultiConditionDemo(); new Thread(() -> demo.producer()).start(); new Thread(() -> demo.consumer()).start(); @@ -272,4 +271,4 @@ public class TestCacheThreadPool { } -} \ No newline at end of file +} diff --git a/src/main/java/cn/whaifree/test/mulThread/AtomicStampedReferenceDemo.java b/src/main/java/cn/whaifree/test/mulThread/AtomicStampedReferenceDemo.java new file mode 100644 index 0000000..91c21fe --- /dev/null +++ b/src/main/java/cn/whaifree/test/mulThread/AtomicStampedReferenceDemo.java @@ -0,0 +1,63 @@ +package cn.whaifree.test.mulThread; + +import java.util.concurrent.atomic.AtomicStampedReference; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/21 21:08 + * @注释 + */ +public class AtomicStampedReferenceDemo { + + static class User{ + private String name; + private int age; + + public User(String name, int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + } + + public static void main(String[] args) { + + User user = new User("张三", 18); + AtomicStampedReference atomicStampedReference = new AtomicStampedReference<>(user, 1); + for (int i = 0; i < 10; i++) { + User newUser = new User("李四", 20); + boolean result = update(atomicStampedReference, newUser); + System.out.println(result); + } + + } + + + public static boolean update(AtomicStampedReference atomicStampedReference, User newUser) { + User oldUser = atomicStampedReference.getReference(); + int stamp = atomicStampedReference.getStamp(); + + while (!atomicStampedReference.compareAndSet(oldUser, newUser, stamp, stamp + 1)) { + // 获取失败,更新User和stamp + oldUser = atomicStampedReference.getReference(); + stamp = atomicStampedReference.getStamp(); + } + return true; + } +} diff --git a/src/main/java/cn/whaifree/test/threadpool/ScheduledThreadPoolTest.java b/src/main/java/cn/whaifree/test/threadpool/ScheduledThreadPoolTest.java new file mode 100644 index 0000000..6bafa7f --- /dev/null +++ b/src/main/java/cn/whaifree/test/threadpool/ScheduledThreadPoolTest.java @@ -0,0 +1,49 @@ +package cn.whaifree.test.threadpool; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/21 16:40 + * @注释 + */ +public class ScheduledThreadPoolTest { + volatile static Integer i = 0; + + public static void main(String[] args) { + + ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10); + scheduledExecutorService.schedule(new Runnable() { + @Override + public void run() { + System.out.println("delay:" + i); + } + }, 10, TimeUnit.SECONDS); + // 10秒后才执行 + scheduledExecutorService.shutdown(); + + + ExecutorService executorService = Executors.newSingleThreadExecutor(); + for (int j = 0; j < 10; j++) { + int finalJ = j; + executorService.submit(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + System.out.println(finalJ); + } + }); + } + + + + } +}