目前没通过;https://leetcode-cn.com/problems/3sum/
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int cmp(const void *_a, const void *_b) {
return (*(int*)_a) - (*(int *)_b);
}
void addElement(int *nums, int i,int j, int k, int **res, int sub) {
int *ele = malloc(sizeof (int) * 3);
ele[0] = nums[i];
ele[1] = nums[j];
ele[2] = nums[k];
res[sub] = ele;
}
int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
char **res;
int i, j, k;
*returnSize = 0;
if (numsSize <3) return NULL;
// 作者:Brad159
// 链接:https://leetcode-cn.com/problems/3sum/solution/xun-huan-yu-pan-duan-bi-jiao-zhi-guan-de-xie-fa-ji/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
res = malloc(sizeof (char *) * 10000);
memset(res, 0, 10000);
qsort(nums, numsSize, sizeof(int), cmp);
for (i=0; i<numsSize-2; ++i) {
if (i!=0 && nums[i] == nums[i-1]) {
continue;
}
int jVlaue = nums[i+1];
for (j=i+1; j<numsSize-1; ++j) {
if (j!= i+1 && jVlaue == nums[j]) {
continue;
}
int kValue = nums[j+1];
for (k=j+1; k<numsSize; ++k) {
if (k != j+1 && nums[k] == kValue) {
continue;
}
if (nums[i] + nums[j] + nums[k] == 0) {
addElement(nums, i, j, k, res, *returnSize);
*returnSize = *returnSize + 1;
}
}
}
}
return res;
}