leetcode-two sum
/** * Note: The returned array must be malloced, assume caller calls free(). */ typedef struct Element{ int val; int idx; } Ele; int cmp(const void *a,const void *b){ return ((Ele *)a)->val-((Ele *)b)->val; } int* twoSum(int* nums, int numsSize, int target) { int i,j,*result; Ele *list=(Ele*)malloc(sizeof(Ele)*numsSize); result=(int *)malloc(sizeof(int)*2); for(i=0;i<numsSize;i++){ list[i].val=nums[i]; list[i].idx=i; } qsort(list,numsSize,sizeof(Ele),cmp); i=0;j=numsSize-1; while(i<j){ if(list[i].val+list[j].val>target){ j--;continue; } if(list[i].val+list[j].val<target){ i++;continue; } if(list[i].val+list[j].val==target){ result[0]=list[i].idx;result[1]=list[j].idx; return result; } } }