LeetCode_AlgorithmProblem01,TwoSum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Solution1:枚举法
int* twoSum(int* nums, int numsSize, int target) {
int i=0,j=0;
int *twosum = (int *)malloc(sizeof(int) * 2);
while(i<numsSize){
j=i+1;
while(j<numsSize){
if(nums[i]+nums[j]==target){
printf("%2d%2d",i,j);
twosum[0]=i;
twosum[1]=j;
return twosum;
}
j++;
}
i++;
}
return twosum;
}
Result1:89ms/64.95%
Solution2:Hash表法,用target减去num1,然后在Hash表中查找是否存在num2。
int* twoSum(int* nums, int numsSize, int target) {
typedef struct num{
int value;
int pos;
} Nums;
Nums *nums_hash=(Nums*)malloc(sizeof(Nums)*numsSize);
int k=0;
for(k=0;k<numsSize;k++)
nums_hash[k].value=0;
nums_hash[k].pos=0;
int *twosum=(int*)malloc(sizeof(int)*2);
//为数组建立Hash表
int i=0;
while(i<numsSize){
int temp=abs(nums[i]);
if( nums_hash[temp%numsSize].value==0)
{
nums_hash[temp%numsSize].value=nums[i];
nums_hash[temp%numsSize].pos=i;
}
else{
while(1){
if(nums_hash[temp%numsSize].value==0){
nums_hash[temp%numsSize].value=nums[i];
nums_hash[temp%numsSize].pos=i;
break;
}
temp++;
}
}
i++;
}
//Hash表中查找
int j=0;
i=0;
int key;
while(i<numsSize){
int key=target-nums[i];
if(nums_hash[abs(key)%numsSize].value==key&&i!=nums_hash[abs(key)%numsSize].pos){
twosum[0]=i,twosum[1]=nums_hash[abs(key)%numsSize].pos;
// printf("%2d,%d",twosum[0],twosum[1]);
return twosum;
}
else{
j=abs(key)%numsSize+1;
while(j<numsSize){
if(nums_hash[j].value==key){
twosum[0]=i,twosum[1]=nums_hash[j].pos;
// printf("%2d,%d",twosum[0],twosum[1]);
return twosum;
}
j++;
}
}
i++;
}
return twosum;
}
Result2:59ms/82.37%
Solution3:HashMap法
typedef struct{
int key;
int val;
}DataType;
typedef struct{
DataType data;
struct HashNode *next;
}HashNode;
typedef struct{
int size;
HashNode *table;
}HashMap,*hashmap;
//f1_createHashMap
HashMap *CreateHashMap(int *nums,int size){
HashMap *hashmap=(HashMap*)malloc(sizeof(HashMap));
hashmap->size=2*size;
hashmap->table=(HashNode *)malloc(sizeof(HashNode)*hashmap->size);
int j=0;
for(j=0;j<hashmap->size;j++){
hashmap->table[j].data.val=INT_MIN;
hashmap->table[j].next=NULL;
}
int i=0;
while(i<size){
int pos=abs(nums[i])%hashmap->size;
if(hashmap->table[pos].data.val==INT_MIN){
hashmap->table[pos].data.key=i;
hashmap->table[pos].data.val=nums[i];
}
else{
HashNode *lnode=(HashNode*)malloc(sizeof(HashNode)),*hashnode;
lnode->data.key=i;
lnode->data.val=nums[i];
lnode->next=NULL;
hashnode=&(hashmap->table[pos]);
while(hashnode->next!=NULL){
hashnode=hashnode->next;
}
hashnode->next=lnode;
}
i++;
}
return hashmap;
}
//f2_PrintHashMap
void PrintHashMap(HashMap* hashmap){
int i=0;
HashNode *pointer;
while(i<hashmap->size){
pointer=&(hashmap->table[i]);
while(pointer!=NULL){
printf("%10d",pointer->data.val);
pointer=pointer->next;
}
i++;
printf("\n");
}
}
//f3_GetHashNode
int* Get(int value,HashMap hashmap){
int pos=abs(value)%hashmap.size;
HashNode *pointer=&(hashmap.table[pos]);
int i=0;
while(pointer!=NULL){
if(pointer->data.val==value)
return pointer->data.key;
else
pointer=pointer->next;
}
return -1;
}
int* twoSum(int* nums, int numsSize, int target) {
int *twosum=(int *)malloc(sizeof(int)*2);
HashMap *hashmap=CreateHashMap(nums,numsSize);
int i=0;
while(i<numsSize){
int goal=target-nums[i];
if(Get(goal,*hashmap)!=-1&&Get(goal,*hashmap)!=i){
twosum[0]=i,twosum[1]=Get(goal,*hashmap);
break;
}
i++;
}
return twosum;
}
Result3:3ms/89.97%
HashMap数据结构如下图: