From 70816b427e2b041d3510799979c6c91b96c8e4ce Mon Sep 17 00:00:00 2001 From: whaifree <49432110+whaibetter@users.noreply.github.com> Date: Mon, 3 Jun 2024 10:21:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=BA=94=E4=B8=AALe?= =?UTF-8?q?etCode=E9=97=AE=E9=A2=98=E7=9A=84=E8=A7=A3=E5=86=B3=E6=96=B9?= =?UTF-8?q?=E6=A1=88=EF=BC=8C=E4=BC=98=E5=8C=96=E4=BA=86=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E3=80=82=20LeetCode15=5F$?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../redo/redo_all_240511/LeetCode142.java | 43 +++++++++++++ .../redo/redo_all_240511/LeetCode15_$.java | 60 +++++++++++++++++ .../redo/redo_all_240511/LeetCode202.java | 52 +++++++++++++++ .../redo/redo_all_240511/LeetCode2487.java | 54 ++++++++++++++++ .../redo/redo_all_240511/LeetCode287.java | 64 +++++++++++++++++++ 5 files changed, 273 insertions(+) create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode142.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode15_$.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode202.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode2487.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode287.java diff --git a/src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode142.java b/src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode142.java new file mode 100644 index 0000000..f0e4c37 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode142.java @@ -0,0 +1,43 @@ +package cn.whaifree.redo.redo_all_240511; + +import cn.whaifree.leetCode.model.ListNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/6/2 14:49 + * @注释 + */ +public class LeetCode142 { + + @Test + public void test() { + ListNode node = ListNode.listNodeFromArray(new int[]{1,3,2}); + node.next.next.next = node.next; + System.out.println(new Solution().detectCycle(node).val); + } + + + public class Solution { + public ListNode detectCycle(ListNode head) { + ListNode fast = head; + ListNode slow = head; + do { + if (fast == null || fast.next == null) { + return null; + } + fast = fast.next.next; + slow = slow.next; + } while (fast != slow); + + + fast = head; + while (fast != slow) { + fast = fast.next; + slow = slow.next; + } + return fast; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode15_$.java b/src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode15_$.java new file mode 100644 index 0000000..0696bf3 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode15_$.java @@ -0,0 +1,60 @@ +package cn.whaifree.redo.redo_all_240511; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * FIXME: 24-6-2 + * + * @version 1.0 + * @Author whai文海 + * @Date 2024/6/2 20:43 + * @注释 + */ +public class LeetCode15_$ { + + @Test + public void test() { + int[] nums = {-1, 0, 1, 2, -1, -4}; + Solution solution = new Solution(); + List> lists = solution.threeSum(nums); + System.out.println(lists); + } + + class Solution { + public List> threeSum(int[] nums) { + List> result = new ArrayList<>(); + Arrays.sort(nums); + + for (int i = 0; i < nums.length - 2; i++) { + if (i > 0 && nums[i] == nums[i - 1]) { + continue; + } + int left = i + 1; + int right = nums.length - 1; + while (left < right) { + int sum = nums[i] + nums[left] + nums[right]; + if (sum == 0) { + result.add(Arrays.asList(nums[i], nums[left], nums[right])); + while (left < right && nums[left] == nums[left + 1]) { + left++; + } + while (left < right && nums[right] == nums[right - 1]) { + right--; + } + left++; + right--; + } else if (sum < 0) { + left++; + } else { + right--; + } + } + } + return result; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode202.java b/src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode202.java new file mode 100644 index 0000000..f52e9fc --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode202.java @@ -0,0 +1,52 @@ +package cn.whaifree.redo.redo_all_240511; + +import org.junit.Test; + +import java.util.HashSet; +import java.util.Set; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/6/2 16:25 + * @注释 + */ +public class LeetCode202 { + + @Test + public void test() { + Solution solution = new Solution(); + boolean happy = solution.isHappy(19); + System.out.println(happy); + } + + class Solution { + /** + * 2 4 16 37 56 61 37 出现循环 + * @param n + * @return + */ + public boolean isHappy(int n) { + int tmp = n; + Set set = new HashSet<>(); + while (tmp != 1) { + set.add(tmp); + tmp = get(tmp); + if (set.contains(tmp)) { + return false; + } + } + return true; + } + + public int get(int num) { + int sum = 0; + while (num != 0) { + int retail = num % 10; + num /= 10; + sum += retail * retail; + } + return sum; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode2487.java b/src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode2487.java new file mode 100644 index 0000000..cf7cbca --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode2487.java @@ -0,0 +1,54 @@ +package cn.whaifree.redo.redo_all_240511; + +import cn.whaifree.leetCode.model.ListNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/6/2 16:34 + * @注释 + */ +public class LeetCode2487 { + @Test + public void test() { + ListNode listNode = ListNode.listNodeFromArray(new int[]{1, 1, 1, 1}); + new Solution().removeNodes(listNode).printList(); + } + + class Solution { + public ListNode removeNodes(ListNode head) { + head = reverse(head); + // 如果左侧会更大,前移动 + // 如果左侧会更小,删除左侧 + ListNode tmp = head; + ListNode pre = head; + while (tmp!= null) { + // 找到比pre小的 + if (tmp.val < pre.val) { + pre.next = tmp.next; + }else { + pre = tmp; + } + tmp = tmp.next; + } + + + return reverse(head); + } + + public ListNode reverse(ListNode head) { + return reverseList(null, head); + } + + public ListNode reverseList(ListNode pre, ListNode after) { + if (after == null) { + return pre; + } + ListNode tmp = after.next; + after.next = pre; + return reverseList(after, tmp); + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode287.java b/src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode287.java new file mode 100644 index 0000000..5077681 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240511/LeetCode287.java @@ -0,0 +1,64 @@ +package cn.whaifree.redo.redo_all_240511; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/6/2 14:37 + * @注释 + */ +public class LeetCode287 { + + @Test + public void test() { + int[] nums = {3, 1, 3, 4, 2}; + Solution solution = new Solution(); + int i = solution.findDuplicate(nums); + System.out.println(i); + } + + class Solution { + /** + * 成环路 + * + * 1 3 4 2 2 + * 0 1 2 3 4 + * + * 0--->1--->3--->2--->4--->2 + * + * 3 1 3 4 2 + * 0 1 2 3 4 + * + * 0->3-->4-->2 + * + * 快慢指针 + *,类似链表操作 + * i=0 + * next i=nums[i] + * nexr.next i=nums[nums[i]] + * + * @param nums + * @return + */ + public int findDuplicate(int[] nums) { + int fast = 0; + int slow = 0; + do { + fast = nums[nums[fast]]; + slow = nums[slow]; + } while (fast != slow); + + // 找到相交点 + fast = 0; + while (fast != slow) { + fast = nums[fast]; + slow = nums[slow]; + } + + return fast; + } + } + + +}