May anyone could tel lmewhy LeetCode's answer is different with my laptop's? Thanks a lot~~


  • 0
    S

    In case of [1,2,-2,-1]. I don't understand why after Leetcode executed my solution ,gave me [[-2,2,0],[-1,1,0]].
    But my compiler gave me [ ].
    Could anyone help me, thank you so very much~~~

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    void swap(int *a, int *b)
    {
    	int temp=0;
    	temp = *a;
    	*a = *b;
    	*b = temp;
    }
    void myqsort(int* nums, int left, int right)
    {
    	int key = 0, i = left, j = right+1;
    
    	if (left < right)
    	{
    		while (i < j)
    		{
    			key = nums[left];
    			while (nums[++i] < key)
    			{
    				if (i == right)
    					break;
    			}
    			while (nums[--j] > key)
    			{
    				if (j == 0)
    					break;
    			}
    			
    			if (i < j)
    			{	
    				swap(nums + i, nums + j);
    			}
    		}
    		swap(nums+left, nums+j);
    		myqsort(nums, left, j - 1);
    		myqsort(nums, j + 1, right);
    	}
    }
    bool re_ans(int a ,int b ,int c, int**ans,int *size)
    {
    	bool flag = true;
    	int k = (*size);
    	if (k == 0)
    	{
    		flag=true;
    	}
    	else
    	{ 
    		for (int i = 0; i < k; i++)
    		{
    			if (a == ans[i][0] && b == ans[i][1] && c == ans[i][2])
    			{
    				flag=false;
    			}
    		}
    	}
    	return flag;
    }
    int ** twoSume(int* subarray, int target, int size,int* count)
    {
    	(*count) = 0;
    	int ** subAns = (int**)malloc(200 * sizeof(int*));
    	int i = 0;
    	int subtarget = 0,j = 0;
    	for (i = 0; i < 200; i++)
    	{
    		subAns[i] = (int*)malloc(2 * sizeof(int));
    		memset(subAns[i], '\0', 2);
    	}
    	for (i = 0; i < size; i++)
    	{
    		subtarget = target - subarray[i];
    		for (j = i+1; j < size; j++)
    		{
    			if (subarray[j] == subtarget && j != i)
    			{
    				subAns[(*count)][0] = subarray[i];
    				subAns[(*count)][1] = subarray[j];
    				(*count)++;
    				if ((*count) == 20)
    				{
    					printf("BOOM!!!!\n");
    				}
    			}
    		}
    	}
    	subAns[(*count)][0] = -1;
    	subAns[(*count)][1] = -1;
    	return subAns;
    }
    int** threeSum(int* nums, int numsSize, int* returnSize) {
    	//sort first 
    	*returnSize = 0;
    	myqsort(nums, 0, numsSize-1);
    	int i = 0;
    	for (i = 0; i<numsSize; i++)
    		printf("%d ", nums[i]);
    	printf("\n");
    	int** ans = (int**)malloc(200 * sizeof(int*));
    	int ** subAns = (int**)malloc(200 * sizeof(int*));
    	for (i = 0; i < 200; i++)
    	{
    		subAns[i] = (int*)malloc(2 * sizeof(int));
    		memset(subAns[i], '\0', 2);
    	}
    	int ans_index = 0, temp_index = 0,count=0;
    	for (i = 0; i < 200; i++)
    	{
    		ans[i] = (int*)malloc(3 * sizeof(int));
    		memset(ans[i], '\0', 3);
    	}
    	ans_index = 0;
    	for (i = 0; i < numsSize; i++)
    	{
    		temp_index = 0;
    		count = 0;
    		if (numsSize - i >= 2) {
    			subAns = twoSume(nums + 1 + i, nums[i] * -1, numsSize - i, &count);
    			while (count > 0)
    			{
    				count--;
    				if (re_ans(nums[i], subAns[count][0], subAns[count][1], ans, &ans_index))
    				{
    					ans[ans_index][0] = nums[i];
    					ans[ans_index][1] = subAns[count][0];
    					ans[ans_index][2] = subAns[count][1];
    					ans_index++;
    				}
    			}
    		}
    	}
    	*returnSize = ans_index;
    	return ans;
    }
    void main()
    {
    	int array[4] = {1,2,2,-1};
    	int * returnSize = (int*)malloc(1 * sizeof(int));
    	*returnSize = 0;
    	int** ans = (int**)malloc(20 * sizeof(int*));
    	for (int i = 0; i < 20; i++)
    	{
    		ans[i] = (int*)malloc(3 * sizeof(int));
    		memset(ans[i], '0', 3);
    	}
    	ans = threeSum(array, 4, returnSize);
    	for (int i = 0; i < *returnSize; i++)
    		printf("[%d %d %d]\n", ans[i][0], ans[i][1], ans[i][2]);
    	system("PAUSE");
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.