【leetcode】18. 四數之和
阿新 • • 發佈:2020-12-08
#define MAXLEN 50 int cmp(const void* a, const void* b){ return *(int*)a - *(int*)b; } int** fourSum(int* nums, int numsSize, int target, int* returnSize, int** returnColumnSizes){ int first=0, secend, left, right, temp; *returnSize=0; *returnColumnSizes=(int*)calloc(MAXLEN,sizeof(int)); int** arr=(int**)calloc(MAXLEN,sizeof(int*)); qsort(nums, numsSize, sizeof(int), cmp); while(first<numsSize-3 ){ secend=first+1; while (secend < numsSize - 2 ){ left = secend + 1; right = numsSize - 1; while (left < right){ temp= nums[first] + nums[secend] + nums[left] + nums[right]; if (temp == target){ arr[(*returnSize)]=(int*)calloc(4,sizeof(int)); arr[(*returnSize)][0]=nums[first]; arr[(*returnSize)][1]=nums[secend]; arr[(*returnSize)][2]=nums[left]; arr[(*returnSize)][3]=nums[right]; (*returnColumnSizes)[(*returnSize)++]=4; while (nums[left] == nums[++left] && left < right); while (nums[right] == nums[--right] && left < right); } else if (temp < target){ while (nums[left] == nums[++left] && left < right); } else{ while (nums[right] == nums[--right] && left < right); } } while (nums[secend] == nums[++secend] && secend < numsSize - 2); } while (nums[first] == nums[++first] && first < numsSize - 3); } return arr; }