It is always notice me that "Submission Result: Runtime Error Last executed input: [3,2,4], 6"?? why??


  • 0
    H

    It is my code!!!!
    I'm sooooooo angry!!!!! shit!

    in my ubuntu the answer is index1=2 index2=3 !!!!!!!!! 2+4 = 6!!!it's Truth!

    int bs(const int* array,int n,int value){
        int start = 0;
        int end = n-1;
        while(start<=end){
                int mid = (start+end)/2;
                if(value == array[mid]){
                        return mid;
                } else if(value > array[mid]){
                        start = mid+1;
                } else {
                        end = mid-1;
                }
        }
        return -1;
    

    }

    int *twoSum(int numbers[], int n, int target) {
      int *a = (int*)malloc(sizeof(int)*2);
      int* tmp = (int*)malloc(sizeof(int)*n);
        int j = 0;
        int i = 0;
        for(i=0;i<n;i++){
                tmp[i] = target - numbers[i];
        }
        for(j=0;j<n;j++){
                int k = bs(numbers,n,tmp[j]);
                if(-1 == k){
                        continue;
                } else {
                        a[0] = j+1;
                        a[1] = k+1;
                        free(tmp);
                        break;
                }
        }
        a[0] = -1;
        a[1] = -1;
        free(tmp);
        return a;
    

    }


Log in to reply
 

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