From 5e355d9935ff36e7094d8c775266d6d683200d5f Mon Sep 17 00:00:00 2001 From: fanxb Date: Fri, 2 Apr 2021 15:49:18 +0800 Subject: [PATCH] add --- 5.leetcode/src/com/fanxb/common/Q142.java | 62 +++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q167.java | 36 +++++++++++++ 5.leetcode/src/com/fanxb/common/Q88.java | 41 +++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 5.leetcode/src/com/fanxb/common/Q142.java create mode 100644 5.leetcode/src/com/fanxb/common/Q167.java create mode 100644 5.leetcode/src/com/fanxb/common/Q88.java diff --git a/5.leetcode/src/com/fanxb/common/Q142.java b/5.leetcode/src/com/fanxb/common/Q142.java new file mode 100644 index 0000000..c716ccf --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q142.java @@ -0,0 +1,62 @@ +package com.fanxb.common; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +/** + * 合并两个有序数组 + * 题目地址:https://leetcode-cn.com/problems/merge-sorted-array/ + * 解题思路: + * + * @author fanxb + * Date: 2020/6/9 15:10 + */ +public class Q142 { + + public static class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + next = null; + } + } + + public static ListNode solution(ListNode head) { + Set set = new HashSet<>(); + while (head != null) { + if (set.contains(head)) { + return head; + } else { + set.add(head); + head = head.next; + } + } + return null; + } + + public static ListNode betterSolution(ListNode head) { + ListNode slow = head, fast = head; + do { + if (fast == null || fast.next == null) { + return null; + } + slow = slow.next; + fast = fast.next.next; + } while (slow != fast); + //此时快慢指针相遇,再加一个指针c + ListNode c = head; + while (c != slow) { + slow = slow.next; + c = c.next; + } + return c; + } + + + public static void main(String[] args) { + System.out.println(); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q167.java b/5.leetcode/src/com/fanxb/common/Q167.java new file mode 100644 index 0000000..338e175 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q167.java @@ -0,0 +1,36 @@ +package com.fanxb.common; + +import java.util.Arrays; + +/** + * 两数之和 II - 输入有序数组 + *

+ * 题目地址:https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/ + *

+ * 解题思路: + * 两个指针从两端开始望中间搜索 + * + * @author fanxb + * Date: 2020/6/9 15:10 + */ +public class Q167 { + + public static int[] solution(int[] numbers, int target) { + int i = 0, j = numbers.length - 1; + int num; + while ((num = numbers[i] + numbers[j]) != target) { + if (num > target) { + j--; + } else { + i++; + } + } + return new int[]{i+1, j+1}; + } + + + public static void main(String[] args) { + int[] numbers = {2, 7, 11, 15}; + System.out.println(Arrays.toString(solution(numbers, 9))); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q88.java b/5.leetcode/src/com/fanxb/common/Q88.java new file mode 100644 index 0000000..d3dc461 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q88.java @@ -0,0 +1,41 @@ +package com.fanxb.common; + +import java.util.Arrays; +import java.util.Collections; + +/** + * 合并两个有序数组 + * 题目地址:https://leetcode-cn.com/problems/merge-sorted-array/ + * 解题思路: + * + * @author fanxb + * Date: 2020/6/9 15:10 + */ +public class Q88 { + + public static void solution(int[] nums1, int m, int[] nums2, int n) { + int[] res = new int[nums1.length]; + int i = 0, j = 0; + int count = 0; + while (count < m + n) { + if (i == m) { + res[count++] = nums2[j++]; + } else if (j == n) { + res[count++] = nums1[i++]; + } else if (nums1[i] < nums2[j]) { + res[count++] = nums1[i++]; + } else { + res[count++] = nums2[j++]; + } + } + System.arraycopy(res, 0, nums1, 0, count); + } + + + public static void main(String[] args) { + int[] nums1 = {1, 2, 3, 0, 0, 0}; + int[] nums2 = {2, 5, 6}; + solution(nums1, 3, nums2, 3); + System.out.println(Arrays.toString(nums1)); + } +}