diff --git a/src/main/java/cn/whaifree/redo/LeetCode15.java b/src/main/java/cn/whaifree/redo/LeetCode15.java new file mode 100644 index 0000000..4bab475 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/LeetCode15.java @@ -0,0 +1,59 @@ +package cn.whaifree.redo; + + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class LeetCode15 { + + @Test + public void test() { + int[] nums = {1,0,0}; + List> lists = new Solution().threeSum(nums); + System.out.println(lists); + } + + + class Solution { + public List> threeSum(int[] nums) { + + Arrays.sort(nums); + + List> lists = new ArrayList<>(); + // 计算三个数的和 + for (int i = 0; i < nums.length; i++) { +// + if (i != 0 && nums[i - 1] == nums[i]) { // 第一次不进入循环,i!=0 + continue; + } + + int left = i + 1; + int right = nums.length - 1; + while (left < right) { + int sum = nums[left] + nums[right] + nums[i]; + if (sum > 0) { + right--; + } else if (sum < 0) { + left++; + } else { + lists.add(Arrays.asList(nums[left], nums[right], nums[i])); + left++; + right--; + while (left < right && nums[left] == nums[left - 1]) { // 去重,且避免数组都是一个数字的情况 + left++; + } + while (left < right && nums[right] == nums[right + 1]) { // 去重,且避免数组都是一个数字的情况 + right--; + } + + } + + } + } + return lists; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/LeetCode202.java b/src/main/java/cn/whaifree/redo/LeetCode202.java new file mode 100644 index 0000000..99694c6 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/LeetCode202.java @@ -0,0 +1,43 @@ +package cn.whaifree.redo; + +import org.junit.Test; + +import java.util.HashSet; +import java.util.Set; + +public class LeetCode202 { + + @Test + public void test() { + System.out.println(new Solution().isHappy(2)); + } + + class Solution { + /** + * 无限循环 + * @param n + * @return + */ + public boolean isHappy(int n) { + Set set = new HashSet<>(); + while (n != 1) { + if (set.contains(n)) { + return false; + } + set.add(n); + n = get(n); + } + return true; + } + + public int get(int n) { + int sum = 0; + while (n != 0) { + int e = n % 10; + sum += e * e; + n /= 10; + } + return sum; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/LeetCode2487.java b/src/main/java/cn/whaifree/redo/LeetCode2487.java new file mode 100644 index 0000000..7fded65 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/LeetCode2487.java @@ -0,0 +1,62 @@ +package cn.whaifree.redo; + +import cn.whaifree.leetCode.model.ListNode; +import org.junit.Test; + +public class LeetCode2487 { + + @Test + public void test() { + ListNode head = ListNode.listNodeFromArray(new int[]{5, 2, 13, 3, 8}); + ListNode reverse = new Solution().removeNodes(head); + reverse.printList(); + + } + + /** + * + * 移除每个右侧有一个更大数值的节点。 + * [5,2,13,3,8] + * 输出:[13,8] + * + * - 递减的 + * + * + * + */ + class Solution { + public ListNode removeNodes(ListNode head) { + // 逆转 8 3 13 2 5 + head = reverse(head); + // 删除全部比当前点小的值 + ListNode index = head; + ListNode maxNode = head; + while (index.next != null) { + if (maxNode.val > index.next.val) { + index.next = index.next.next; + } else { + index = index.next; + maxNode = index; + } + } + + return head; + } + + + // 1 2 3 4 5 + public ListNode reverse(ListNode head) { + ListNode pre = null; + ListNode index = head; + ListNode tmp = null; + while (index != null) { + tmp = index.next; + index.next = pre; + pre = index; + index = tmp; + } + return pre; + } + + } +} diff --git a/src/main/java/cn/whaifree/redo/LeetCode287.java b/src/main/java/cn/whaifree/redo/LeetCode287.java new file mode 100644 index 0000000..61df1fd --- /dev/null +++ b/src/main/java/cn/whaifree/redo/LeetCode287.java @@ -0,0 +1,51 @@ +package cn.whaifree.redo; + +import cn.hutool.core.lang.hash.Hash; +import org.junit.Test; + +import java.util.HashSet; +import java.util.Set; + +public class LeetCode287 { + + @Test + public void test() { + System.out.println(new Solution().findDuplicate(new int[]{1, 3, 4, 3, 2})); + } + + class Solution { + /** + * 要求 O(N)的时间复杂度 + * O(1)的控空间 + * + * 将数组转换为链表的思想,即求有没有形成环 + * 1 3 4 2 2 + * 0 1 2 3 4 + * 0-->1-->2-->4--->2 + * + * @param nums + * @return + */ + public int findDuplicate(int[] nums) { + // 1. 找到相遇的点 + // 2. 将快指针指向首部 + // 3. 两个指针重合处就是 + + 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; + } + } +}