C code ,I dont know what mean "2 points".


  • -1
    8
    void quicksort(int a[],int l,int h)
    {
    	if(l<h){
    		int i = l;
    		int j = h;
    		int t = a[l];
    		while(j > i){
    			while(j > i && a[j] >= t) j --;
    			if(j > i) a[i++] = a[j];
    			while(i < j && a[i] <= t) i ++;
    			if(j > i) a[j--] = a[i];
    		}
    		a[i] = t;
    		quicksort(a,l,i-1);
    		quicksort(a,i+1,h);
    	}
    }
    
    int towPoints(int * ns,int p,int q,int tag,int *rs,int** ret)
    {
    	while(p < q){
    		int t = ns[p]+ns[q];
    		if(t == tag){
    			*(ret+*rs) = (int *)malloc(3*sizeof(int));
    			**(ret+*rs) = -1*tag;
    			*(*(ret+*rs)+1) = ns[p];
    			*(*(ret+*rs)+2) = ns[q];
    			*rs = *rs + 1;
    			while(p+1 < q && ns[p] == ns[p+1]) p ++;p ++;
    			while(p < q-1 && ns[q] == ns[q-1]) q --;q --;
    		}else if(t < tag){
    			p ++;
    		}else{
    			q --;
    		}
    	}
    	return 0;
    }
    
    int** threeSum(int* nums, int numsSize, int* rs) {
    	*rs = 0;
        if(numsSize < 3) return NULL;
        quicksort(nums,0,numsSize-1);
    	int k = 0;
    	int ** ret = (int **)malloc(3000*sizeof(int));
        while(k < numsSize && nums[k] <= 0){
        	towPoints(nums,k+1,numsSize-1,nums[k]*-1,rs,ret);
        	while(k+1 < numsSize && nums[k] == nums[k+1]) k++;
        	k ++;
    	}
    	return ret;
    }

  • 0
    F

    Did you mean "Two pointers"?


  • 0
    8

    yes,what's the point of "tow points"?


Log in to reply
 

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