Skip to content

Commit a34c543

Browse files
Merge pull request youngyangyang04#2428 from Heeqw/master
Update 面试题02.07.链表相交.md
2 parents ac4e77f + 57cf171 commit a34c543

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

problems/0001.两数之和.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public:
133133
### Java:
134134
135135
```java
136+
//使用哈希表
136137
public int[] twoSum(int[] nums, int target) {
137138
int[] res = new int[2];
138139
if(nums == null || nums.length == 0){
@@ -151,6 +152,43 @@ public int[] twoSum(int[] nums, int target) {
151152
return res;
152153
}
153154
```
155+
```java
156+
//使用双指针
157+
public int[] twoSum(int[] nums, int target) {
158+
int m=0,n=0,k,board=0;
159+
int[] res=new int[2];
160+
int[] tmp1=new int[nums.length];
161+
//备份原本下标的nums数组
162+
System.arraycopy(nums,0,tmp1,0,nums.length);
163+
//将nums排序
164+
Arrays.sort(nums);
165+
//双指针
166+
for(int i=0,j=nums.length-1;i<j;){
167+
if(nums[i]+nums[j]<target)
168+
i++;
169+
else if(nums[i]+nums[j]>target)
170+
j--;
171+
else if(nums[i]+nums[j]==target){
172+
m=i;
173+
n=j;
174+
break;
175+
}
176+
}
177+
//找到nums[m]在tmp1数组中的下标
178+
for(k=0;k<nums.length;k++){
179+
if(tmp1[k]==nums[m]){
180+
res[0]=k;
181+
break;
182+
}
183+
}
184+
//找到nums[n]在tmp1数组中的下标
185+
for(int i=0;i<nums.length;i++){
186+
if(tmp1[i]==nums[n]&&i!=k)
187+
res[1]=i;
188+
}
189+
return res;
190+
}
191+
```
154192

155193
### Python:
156194
(版本一) 使用字典

problems/0015.三数之和.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ while (right > left) {
256256
## 其他语言版本
257257

258258
### Java:
259-
259+
(版本一) 双指针
260260
```Java
261261
class Solution {
262262
public List<List<Integer>> threeSum(int[] nums) {
@@ -297,7 +297,43 @@ class Solution {
297297
}
298298
}
299299
```
300+
(版本二) 使用哈希集合
301+
```Java
302+
class Solution {
303+
public List<List<Integer>> threeSum(int[] nums) {
304+
List<List<Integer>> result = new ArrayList<>();
305+
Arrays.sort(nums);
300306

307+
for (int i = 0; i < nums.length; i++) {
308+
// 如果第一个元素大于零,不可能凑成三元组
309+
if (nums[i] > 0) {
310+
return result;
311+
}
312+
// 三元组元素a去重
313+
if (i > 0 && nums[i] == nums[i - 1]) {
314+
continue;
315+
}
316+
317+
HashSet<Integer> set = new HashSet<>();
318+
for (int j = i + 1; j < nums.length; j++) {
319+
// 三元组元素b去重
320+
if (j > i + 2 && nums[j] == nums[j - 1] && nums[j - 1] == nums[j - 2]) {
321+
continue;
322+
}
323+
324+
int c = -nums[i] - nums[j];
325+
if (set.contains(c)) {
326+
result.add(Arrays.asList(nums[i], nums[j], c));
327+
set.remove(c); // 三元组元素c去重
328+
} else {
329+
set.add(nums[j]);
330+
}
331+
}
332+
}
333+
return result;
334+
}
335+
}
336+
```
301337
### Python:
302338
(版本一) 双指针
303339

problems/面试题02.07.链表相交.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public:
105105
### Java:
106106
107107
```Java
108+
(版本一)先行移动长链表实现同步移动
108109
public class Solution {
109110
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
110111
ListNode curA = headA;
@@ -149,6 +150,23 @@ public class Solution {
149150
}
150151
151152
}
153+
154+
(版本二) 合并链表实现同步移动
155+
public class Solution {
156+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
157+
// p1 指向 A 链表头结点,p2 指向 B 链表头结点
158+
ListNode p1 = headA, p2 = headB;
159+
while (p1 != p2) {
160+
// p1 走一步,如果走到 A 链表末尾,转到 B 链表
161+
if (p1 == null) p1 = headB;
162+
else p1 = p1.next;
163+
// p2 走一步,如果走到 B 链表末尾,转到 A 链表
164+
if (p2 == null) p2 = headA;
165+
else p2 = p2.next;
166+
}
167+
return p1;
168+
}
169+
}
152170
```
153171

154172
### Python:

0 commit comments

Comments
 (0)