From abc86e9a7d66851ada0e54f74efd50b9ce5da8f2 Mon Sep 17 00:00:00 2001 From: Heeqw <124508798+Heeqw@users.noreply.github.com> Date: Sat, 27 Jan 2024 17:05:04 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Update=20=E9=9D=A2=E8=AF=95=E9=A2=9802.07.?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加了一种java语言的同步移动方法 --- problems/面试题02.07.链表相交.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/面试题02.07.链表相交.md b/problems/面试题02.07.链表相交.md index d0967b8b..e2905d49 100644 --- a/problems/面试题02.07.链表相交.md +++ b/problems/面试题02.07.链表相交.md @@ -105,6 +105,7 @@ public: ### Java: ```Java +(版本一)先行移动长链表实现同步移动 public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { ListNode curA = headA; @@ -149,6 +150,23 @@ public class Solution { } } + +(版本二) 合并链表实现同步移动 +public class Solution { + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { + // p1 指向 A 链表头结点,p2 指向 B 链表头结点 + ListNode p1 = headA, p2 = headB; + while (p1 != p2) { + // p1 走一步,如果走到 A 链表末尾,转到 B 链表 + if (p1 == null) p1 = headB; + else p1 = p1.next; + // p2 走一步,如果走到 B 链表末尾,转到 A 链表 + if (p2 == null) p2 = headA; + else p2 = p2.next; + } + return p1; + } +} ``` ### Python: From 586b8efd38f2ee919c38b207d7fcb0a442def32c Mon Sep 17 00:00:00 2001 From: Heeqw <124508798+Heeqw@users.noreply.github.com> Date: Mon, 29 Jan 2024 23:39:32 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Update=200001.=E4=B8=A4=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加java语言版本的双指针法 --- problems/0001.两数之和.md | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 1785fe5f..80218cb5 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -133,6 +133,7 @@ public: ### Java: ```java +//使用哈希表 public int[] twoSum(int[] nums, int target) { int[] res = new int[2]; if(nums == null || nums.length == 0){ @@ -151,6 +152,43 @@ public int[] twoSum(int[] nums, int target) { return res; } ``` +```java +//使用双指针 +public int[] twoSum(int[] nums, int target) { + int m=0,n=0,k,board=0; + int[] res=new int[2]; + int[] tmp1=new int[nums.length]; + //备份原本下标的nums数组 + System.arraycopy(nums,0,tmp1,0,nums.length); + //将nums排序 + Arrays.sort(nums); + //双指针 + for(int i=0,j=nums.length-1;itarget) + j--; + else if(nums[i]+nums[j]==target){ + m=i; + n=j; + break; + } + } + //找到nums[m]在tmp1数组中的下标 + for(k=0;k Date: Tue, 30 Jan 2024 16:27:08 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Update=200015.=E4=B8=89=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加了java语言版本的哈希解法 --- problems/0015.三数之和.md | 38 ++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 91fc9d68..bf165788 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -256,7 +256,7 @@ while (right > left) { ## 其他语言版本 ### Java: - +(版本一) 双指针 ```Java class Solution { public List> threeSum(int[] nums) { @@ -297,7 +297,43 @@ class Solution { } } ``` +(版本二) 使用哈希集合 +```Java +class Solution { + public List> threeSum(int[] nums) { + List> result = new ArrayList<>(); + Arrays.sort(nums); + for (int i = 0; i < nums.length; i++) { + // 如果第一个元素大于零,不可能凑成三元组 + if (nums[i] > 0) { + return result; + } + // 三元组元素a去重 + if (i > 0 && nums[i] == nums[i - 1]) { + continue; + } + + HashSet set = new HashSet<>(); + for (int j = i + 1; j < nums.length; j++) { + // 三元组元素b去重 + if (j > i + 2 && nums[j] == nums[j - 1] && nums[j - 1] == nums[j - 2]) { + continue; + } + + int c = -nums[i] - nums[j]; + if (set.contains(c)) { + result.add(Arrays.asList(nums[i], nums[j], c)); + set.remove(c); // 三元组元素c去重 + } else { + set.add(nums[j]); + } + } + } + return result; + } +} +``` ### Python: (版本一) 双指针