1. 程式人生 > >leetcode 001 Two Sum

leetcode 001 Two Sum

返回 print pre free inpu public 組元 oid !=

兩數之和。

給一個整數數組,返回兩個數組元素為特定值的數組元素索引。可以假定每個輸入都有且只有一個解,每個數組元素不能使用兩次。

例子:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,

return [0, 1].

二重循環暴力搜索當然是一個辦法,但時間復雜度會很高。因此這道題主要是考察map的使用方法。

C++代碼

 1 class Solution 
 2 {
 3     public:
 4     vector<int> twoSum(vector<int> &numbers, int
target) 5 { 6 //Key is the number and value is its index in the vector. 7 unordered_map<int, int> hash;// map ordered, unordered_map unordered 8 vector<int> result; 9 for (int i = 0; i < numbers.size(); i++) 10 { 11 int numberToFind = target - numbers[i];
12 13 //if numberToFind is found in map, return them 14 if (hash.find(numberToFind) != hash.end()) 15 { 16 //+1 because indices are NOT zero based 17 result.push_back(hash[numberToFind]); 18 result.push_back(i);
19 return result; 20 } 21 22 //number was not found. Put it in the map. 23 hash[numbers[i]] = i; 24 } 25 return result; 26 } 27 };

在定義map的時候我們使用了unordered_map, unordered_map不會根據key的大小進行排序。而在本題中,沒有必使用排序的map,因此為了降低時間復雜度,unordered_map無疑是一個比較好的選擇。

python 3 代碼

 1 class Solution(object):
 2     def twoSum(self, nums, target):
 3         if len(nums) <= 1:
 4             return False
 5         buff_dict = {}
 6         for i in range(len(nums)):
 7             if nums[i] in buff_dict:
 8                 return [buff_dict[nums[i]], i]
 9             else:
10                 buff_dict[target - nums[i]] = i

C 代碼 (使用哈希表,負載因子0.5, 負載因子越大,哈希表越容易滿,越容易引起沖突,性能越差,這是一個用空間換性能的方法)

  1 #include <malloc.h> 
  2 #include <stdlib.h>
  3 #include <stdio.h>
  4 #include<math.h>
  5 
  6 typedef struct HashNode 
  7 {
  8     int key;
  9     int val;
 10 } HashNode;
 11 
 12 typedef struct HashMap 
 13 {
 14     int size;
 15     HashNode** storage;
 16 } HashMap;
 17 
 18 
 19 
 20 HashMap* hash_create(int size)
 21 {
 22     HashMap* hashMap = (HashMap*)malloc(sizeof(HashMap));
 23     hashMap->size = size;
 24     hashMap->storage = (HashNode**)calloc(size, sizeof(HashNode*));
 25     return hashMap;
 26 }
 27 
 28 void hash_destroy(HashMap* hashMap) 
 29 {
 30     int i;
 31     for(i =0; i < hashMap->size; i++) 
 32     {
 33         HashNode *node;
 34         if((node = hashMap->storage[i])) 
 35         {
 36             free(node);
 37         }
 38     }
 39     free(hashMap->storage);
 40     free(hashMap);
 41 }
 42 
 43 void hash_set(HashMap *hashMap, int key, int value) 
 44 {
 45     int hash = abs(key) % hashMap->size;
 46     HashNode* node;
 47     while ((node = hashMap->storage[hash])) 
 48     {
 49         if (hash < hashMap->size - 1) 
 50         {
 51             hash++;
 52         } 
 53         else 
 54         {
 55             hash = 0;
 56         }
 57     }
 58     node = (HashNode*)malloc(sizeof(HashNode));
 59     node->key = key;
 60     node->val = value;
 61     hashMap->storage[hash] = node;
 62 }
 63 
 64 HashNode* hash_get(HashMap *hashMap, int key) 
 65 {
 66     int hash = abs(key) % hashMap->size;
 67     HashNode* node;
 68     while ((node = hashMap->storage[hash])) 
 69     {
 70         if (node->key == key) 
 71         {
 72             return node;
 73         }
 74 
 75         if (hash < hashMap->size - 1) 
 76         {
 77             hash++;
 78         } 
 79         else 
 80         {
 81             hash = 0;
 82         }
 83     }
 84 
 85     return NULL;
 86 }
 87 
 88 int* twoSum(int* nums, int numsSize, int target) 
 89 {
 90     HashMap* hashMap;
 91     HashNode* node;
 92     int rest, i;
 93     int* result = (int*)malloc(sizeof(int)*2);
 94     // make the hashMap 2x size of the numsSize
 95     hashMap = hash_create(numsSize * 2);
 96     for(i = 0; i < numsSize; i++) 
 97     {
 98         rest = target - nums[i];
 99         node = hash_get(hashMap, rest);
100         if (node) 
101         {
102             result[0] = node->val;
103             result[1] = i;
104             hash_destroy(hashMap);
105             return result;
106         } 
107         else 
108         {
109             hash_set(hashMap, nums[i], i);
110         }
111     }
112     return result;
113 }
114 
115 HashMap* hash_create(int size);
116 void hash_destroy(HashMap* hashMap);
117 void hash_set(HashMap* hashMap, int key, int value);
118 HashNode* hash_get(HashMap* hashMap, int key);
119 
120 void main()
121 {
122     int wInput[3] = {3,2,4};
123     int*pwOutput =  twoSum(wInput, 3, 6) ;
124     int wInput1[6] = {10,11,12,13,14,15};
125     int*pwOutput1 =  twoSum(wInput1,6,25) ;
126     printf("%d\n", pwOutput[0]); 
127     printf("%d\n", pwOutput[1]); 
128     printf("%d\n", pwOutput1[0]); 
129     printf("%d\n", pwOutput1[1]); 
130     return;
131 }

leetcode 001 Two Sum