标签:排序 暴力 性能 pytho ack size math.h void type
两数之和。
给一个整数数组,返回两个数组元素为特定值的数组元素索引。可以假定每个输入都有且只有一个解,每个数组元素不能使用两次。
例子:
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 }
标签:排序 暴力 性能 pytho ack size math.h void type
原文地址:http://www.cnblogs.com/Libra-Beta/p/7397228.html