Skip to content

Commit a3863cb

Browse files
committed
add for two-sum
1 parent 460d367 commit a3863cb

14 files changed

+601
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com..houbb.leetcode.datastruct.array;
2+
3+
public class T001_twoSum_V1_BF {
4+
5+
6+
public int[] twoSum(int[] nums, int target) {
7+
int[] res = new int[2];
8+
9+
final int n = nums.length;
10+
for(int i = 0; i < n; i++) {
11+
for(int j = i+1; j < n; j++) {
12+
if(nums[i] + nums[j] == target) {
13+
res[0] = i;
14+
res[1] = j;
15+
}
16+
}
17+
}
18+
19+
return res;
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com..houbb.leetcode.datastruct.array;
2+
3+
import java.util.*;
4+
5+
public class T001_twoSum_V2_SortBinarySearch {
6+
7+
8+
/**
9+
* @param nums
10+
* @param target
11+
* @return
12+
*/
13+
public int[] twoSum(int[] nums, int target) {
14+
int n = nums.length;
15+
16+
// 存储值+下标 避免排序后找不到原始的索引
17+
List<int[]> indexList = new ArrayList<>();
18+
for(int i = 0; i < n; i++) {
19+
indexList.add(new int[]{nums[i], i});
20+
}
21+
Collections.sort(indexList, new Comparator<int[]>() {
22+
@Override
23+
public int compare(int[] o1, int[] o2) {
24+
return o1[0] - o2[0];
25+
}
26+
});
27+
28+
// 遍历+二分 这里直接手写二分比较简单,因为直接查询数字可能会重复
29+
for(int i = 0; i < n-1; i++) {
30+
int t = target - indexList.get(i)[0];
31+
//从当前 i 的后面开始寻找
32+
int j = binarySearch(indexList, t, i+1);
33+
if(j > 0) {
34+
return new int[]{i, j};
35+
}
36+
}
37+
38+
//NOT FOUND
39+
return new int[]{-1, -1};
40+
}
41+
42+
private int binarySearch(List<int[]> indexList,
43+
final int target,
44+
final int startIx) {
45+
int left = startIx;
46+
int right = indexList.size()-1;
47+
while (left <= right) {
48+
int mid = left + (right-left) / 2;
49+
int val = indexList.get(mid)[0];
50+
if(val == target) {
51+
// 原始下标
52+
return indexList.get(mid)[1];
53+
}
54+
55+
// update
56+
if(val < target) {
57+
left = mid+1;
58+
} else {
59+
right = mid-1;
60+
}
61+
}
62+
63+
return -1;
64+
}
65+
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com..houbb.leetcode.datastruct.array;
2+
3+
import java.util.Arrays;
4+
5+
@Deprecated
6+
public class T001_twoSum_V2_SortBinarySearch_ERR {
7+
8+
9+
/**
10+
* 错误的代码
11+
* @param nums
12+
* @param target
13+
* @return
14+
*/
15+
public int[] twoSum(int[] nums, int target) {
16+
int[] res = new int[2];
17+
18+
Arrays.sort(nums);
19+
20+
// 遍历+二分
21+
int n = nums.length;
22+
for(int i = 0; i < n; i++) {
23+
// 找另一部分
24+
int t = target - nums[i];
25+
26+
// 找到了自己怎么办?
27+
int j = Arrays.binarySearch(nums, t);
28+
if(j > 0) {
29+
res[0] = i;
30+
res[1] = j;
31+
return res;
32+
}
33+
}
34+
35+
return res;
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com..houbb.leetcode.datastruct.array;
2+
3+
import java.util.*;
4+
5+
public class T001_twoSum_V3_HashMap {
6+
7+
8+
/**
9+
* @param nums
10+
* @param target
11+
* @return
12+
*/
13+
public int[] twoSum(int[] nums, int target) {
14+
int n = nums.length;
15+
16+
HashMap<Integer, Integer> hashMap = new HashMap<>();
17+
for(int i = 0; i < n; i++) {
18+
int other = target - nums[i];
19+
if(hashMap.containsKey(other)) {
20+
int j = hashMap.get(other);
21+
return new int[]{i, j};
22+
}
23+
// 存储
24+
hashMap.put(nums[i], i);
25+
}
26+
27+
return new int[]{-1, -1};
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com..houbb.leetcode.datastruct.array;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
import java.util.List;
7+
8+
public class T001_twoSum_V3_SortTwoPointer {
9+
10+
11+
/**
12+
* @param nums
13+
* @param target
14+
* @return
15+
*/
16+
public int[] twoSum(int[] nums, int target) {
17+
int n = nums.length;
18+
19+
// 存储值+下标 避免排序后找不到原始的索引
20+
List<int[]> indexList = new ArrayList<>();
21+
for(int i = 0; i < n; i++) {
22+
indexList.add(new int[]{nums[i], i});
23+
}
24+
Collections.sort(indexList, new Comparator<int[]>() {
25+
@Override
26+
public int compare(int[] o1, int[] o2) {
27+
return o1[0] - o2[0];
28+
}
29+
});
30+
31+
// 双指针
32+
int left = 0;
33+
int right = n-1;
34+
while (left < right) {
35+
int sum = indexList.get(left)[0] - indexList.get(right)[0];
36+
37+
if(sum == target) {
38+
return new int[]{indexList.get(left)[1]+1, indexList.get(right)[1]+1};
39+
}
40+
if(sum < target) {
41+
left++;
42+
}
43+
if(sum > target) {
44+
right--;
45+
}
46+
}
47+
48+
//NOT FOUND
49+
return new int[]{-1, -1};
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com..houbb.leetcode.datastruct.array;
2+
3+
public class T167_twoSum_V1_BF {
4+
5+
public int[] twoSum(int[] nums, int target) {
6+
int[] res = new int[2];
7+
8+
final int n = nums.length;
9+
for(int i = 0; i < n; i++) {
10+
for(int j = i+1; j < n; j++) {
11+
if(nums[i] + nums[j] == target) {
12+
res[0] = i+1;
13+
res[1] = j+1;
14+
}
15+
}
16+
}
17+
18+
return res;
19+
}
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com..houbb.leetcode.datastruct.array;
2+
3+
public class T167_twoSum_V2_SortBinarySearch {
4+
5+
public int[] twoSum(int[] nums, int target) {
6+
final int n = nums.length;
7+
for(int i = 0; i < n; i++) {
8+
int other = target - nums[i];
9+
10+
int j = binarySearch(nums, other, i+1);
11+
if(j >= 0) {
12+
return new int[]{i+1, j+1};
13+
}
14+
}
15+
16+
return new int[]{-1, -1};
17+
}
18+
19+
private int binarySearch(int[] nums,
20+
int target,
21+
int startIx) {
22+
int left = startIx;
23+
int right = nums.length-1;
24+
25+
while (left <= right) {
26+
int mid = left + (right - left) / 2;
27+
int val = nums[mid];
28+
if(val == target) {
29+
return mid;
30+
}
31+
if(val > target) {
32+
right = mid-1;
33+
} else {
34+
left = mid+1;
35+
}
36+
}
37+
38+
return -1;
39+
}
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com..houbb.leetcode.datastruct.array;
2+
3+
import java.util.HashMap;
4+
5+
public class T167_twoSum_V3_HashMap {
6+
7+
public int[] twoSum(int[] nums, int target) {
8+
int n = nums.length;
9+
HashMap<Integer, Integer> hashMap = new HashMap<>();
10+
for(int i = 0; i < n; i++) {
11+
int other = target - nums[i];
12+
if(hashMap.containsKey(other)) {
13+
int j = hashMap.get(other);
14+
return new int[]{j+1, i+1};
15+
}
16+
// 存储
17+
hashMap.put(nums[i], i);
18+
}
19+
return new int[]{-1, -1};
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com..houbb.leetcode.datastruct.array;
2+
3+
public class T167_twoSum_V4_TwoPointer {
4+
5+
public int[] twoSum(int[] nums, int target) {
6+
int n = nums.length;
7+
int left = 0;
8+
int right = n-1;
9+
10+
while (left < right) {
11+
int sum = nums[left] + nums[right];
12+
if(sum == target) {
13+
return new int[]{left+1, right+1};
14+
}
15+
if(sum < target) {
16+
left++;
17+
}
18+
if(sum > target) {
19+
right--;
20+
}
21+
}
22+
23+
return new int[]{-1, -1};
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com..houbb.leetcode.datastruct.array;
2+
3+
import com..houbb.leetcode.component.TreeNode;
4+
5+
import java.util.HashSet;
6+
import java.util.Map;
7+
import java.util.Set;
8+
9+
public class T653_twoSumIV_V1_PreOrderHashSet {
10+
11+
/**
12+
* 前序遍历
13+
*
14+
* 数据=》
15+
*
16+
* @param root
17+
* @param k
18+
* @return
19+
*/
20+
public boolean findTarget(TreeNode root, int k) {
21+
// 构建结果列表
22+
Set<Integer> numSet = new HashSet<>();
23+
24+
int[] resFlag = new int[]{1};
25+
resFlag[0] = 0;
26+
preOrderTravel(numSet, root, k, resFlag);
27+
28+
return resFlag[0] != 0;
29+
}
30+
31+
private void preOrderTravel(Set<Integer> numSet,
32+
TreeNode root,
33+
int k,
34+
int[] resFlag) {
35+
if(root == null || resFlag[0] != 0) {
36+
return;
37+
}
38+
39+
// 符合
40+
int value = root.val;
41+
if(numSet.contains(k - value)) {
42+
resFlag[0] = 1;
43+
return;
44+
}
45+
numSet.add(value);
46+
47+
preOrderTravel(numSet, root.left, k, resFlag);
48+
49+
preOrderTravel(numSet, root.right, k, resFlag);
50+
}
51+
52+
53+
}

0 commit comments

Comments
 (0)