mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
@ -133,6 +133,7 @@ public:
|
|||||||
### Java:
|
### Java:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
|
//使用哈希表
|
||||||
public int[] twoSum(int[] nums, int target) {
|
public int[] twoSum(int[] nums, int target) {
|
||||||
int[] res = new int[2];
|
int[] res = new int[2];
|
||||||
if(nums == null || nums.length == 0){
|
if(nums == null || nums.length == 0){
|
||||||
@ -151,6 +152,43 @@ public int[] twoSum(int[] nums, int target) {
|
|||||||
return res;
|
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;i<j;){
|
||||||
|
if(nums[i]+nums[j]<target)
|
||||||
|
i++;
|
||||||
|
else if(nums[i]+nums[j]>target)
|
||||||
|
j--;
|
||||||
|
else if(nums[i]+nums[j]==target){
|
||||||
|
m=i;
|
||||||
|
n=j;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//找到nums[m]在tmp1数组中的下标
|
||||||
|
for(k=0;k<nums.length;k++){
|
||||||
|
if(tmp1[k]==nums[m]){
|
||||||
|
res[0]=k;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//找到nums[n]在tmp1数组中的下标
|
||||||
|
for(int i=0;i<nums.length;i++){
|
||||||
|
if(tmp1[i]==nums[n]&&i!=k)
|
||||||
|
res[1]=i;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Python:
|
### Python:
|
||||||
(版本一) 使用字典
|
(版本一) 使用字典
|
||||||
|
@ -256,7 +256,7 @@ while (right > left) {
|
|||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
### Java:
|
### Java:
|
||||||
|
(版本一) 双指针
|
||||||
```Java
|
```Java
|
||||||
class Solution {
|
class Solution {
|
||||||
public List<List<Integer>> threeSum(int[] nums) {
|
public List<List<Integer>> threeSum(int[] nums) {
|
||||||
@ -297,7 +297,43 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
(版本二) 使用哈希集合
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
public List<List<Integer>> threeSum(int[] nums) {
|
||||||
|
List<List<Integer>> 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<Integer> 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:
|
### Python:
|
||||||
(版本一) 双指针
|
(版本一) 双指针
|
||||||
|
|
||||||
|
@ -105,6 +105,7 @@ public:
|
|||||||
### Java:
|
### Java:
|
||||||
|
|
||||||
```Java
|
```Java
|
||||||
|
(版本一)先行移动长链表实现同步移动
|
||||||
public class Solution {
|
public class Solution {
|
||||||
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
|
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
|
||||||
ListNode curA = headA;
|
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:
|
### Python:
|
||||||
|
Reference in New Issue
Block a user