# 使用查找表的经典题

## 暴力法

### Show me the Code

「C」

 1 int* twoSum(int* nums, int numsSize, int target, int* returnSize){
2     *returnSize = 0;
3     int *res = (int *)malloc(sizeof(int) * 2);
4     if (res == NULL) {
5         return NULL;
6     }
7
8
9     for(int i = 0; i < numsSize - 1; ++i) {
10         for(int j = i + 1; j < numsSize; ++j) {
11             if(nums[i] + nums [j] == target) {
12                 res[0] = i;
13                 res[1] = j;
14                 *returnSize = 2;
15                 return res;
16             }
17         }
18     }
19
20     return NULL;
21 }
View Code
 

「复杂度分析」

## 哈希表

「举例」

### Show me the Code

「C++」

 1 vector<int> twoSum(vector<int>& nums, int target) {
2     unordered_map<int, int> record;
3     for (int i = 0; i < nums.size(); ++i) {
4         int theOther = target - nums[i];
5         if (record.find(theOther) != record.end()) {
6             int res[2] = {i, record[theOther]};
7             return vector<int>(res, res + 2);
8         }
9         record[nums[i]] = i;
10     }
11
12     return {};
13 }
View Code
 

「java」

 1 int[] twoSum(int[] nums, int target) {
2     int len = nums.length;
3     Map<Integer, Integer> record = new HashMap<Integer, Integer>();
4     for (int i = 0; i < nums.length; ++i) {
5         int theOther = target - nums[i];
6         if (record.containsKey(theOther)) {
7             return new int[]{record.get(theOther), i};
8         }
9         record.put(nums[i], i);
10     }
11
12     return new int[0];
13 }
View Code
 

「Python3」

1 def twoSum(self, nums: List[int], target: int) -> List[int]:
2     hashtable = dict()
3     for i, num in enumerate(nums):
4         theOther = target - num
5         if theOther in hashtable:
6             return [hashtable[theOther], i]
7         hashtable[nums[i]] = i
8     return []
View Code
 

「复杂度分析」

## 双指针

### Show me the Code

「C」

 1 int cmp(const void *a, const void *b) {
2     return *(int *)a - *(int *)b;
3 }
4
5 /* 对撞指针获取数组中元素之和等于 target 的两元素 */
6 void getIdxOfNumsByTwoPoints(int *arr, int arrSize, int target, int *l, int *r) {
7     int left = 0, right = arrSize - 1;
8     while (left <= right) {
9         int value = arr[left] + arr[right];
10         if (value < target) {
11             left++;
12         } else if (value > target) {
13             right--;
14         } else {
15             *l = left;
16             *r = right;
17             break;
18         }
19     }
20
21     return;
22 }
23
24 /* 获取第一个元素的下标 */
25 int obtainIdxFromArr(int *arr, int arrSize, int target) {
26     for (int i = 0; i < arrSize; ++i) {
27         if (arr[i] == target) {
28             return i;
29         }
30     }
31
32     return -1;
33 }
34
35 /* 获取第二个元素的下标 */
36 int obtainIdxFromArrNotRepeat(int *arr, int arrSize, int target, int k) {
37     for (int i = 0; i < arrSize; ++i) {
38         if (arr[i] == target && i != k) {
39             return i;
40         }
41     }
42
43     return -1;
44 }
45
46 int* twoSum(int* nums, int numsSize, int target, int* returnSize){
47     *returnSize = 0;
48     int *arr = (int *)malloc(numsSize * sizeof(int));
49     if (arr == NULL) {
50         return NULL;
51     }
52
53     /* 拷贝一份 nums 用于排序查找 */
54     memcpy(arr, nums, numsSize * sizeof(int));
55     qsort(arr, numsSize, sizeof(int), cmp);
56
57     int ret = 0;
58     int one = 0, theOther = 0;
59     getIdxOfNumsByTwoPoints(arr, numsSize, target, &one, &theOther);
60
61     int *res = (int *)malloc(2 * sizeof(int));
62     if (res == NULL) {
63         return NULL;
64     }
65
66     memset(res, -1, sizeof(int) * 2);
67     res[0] = obtainIdxFromArr(nums, numsSize, arr[one]);
68     if (res[0] == -1) {
69         return NULL;
70     }
71
72     res[1] = obtainIdxFromArrNotRepeat(nums, numsSize, arr[theOther], res[0]);
73     if (res[1] == -1) {
74         return NULL;
75     }
76
77     *returnSize = 2;
78     return res;
79 }
View Code

 

「复杂度分析」

## 更多精彩

posted @ 2021-06-12 18:49  公众号程序员小熊  阅读(58)  评论(0编辑  收藏  举报