No 1. twoSum, I think the system have bug


  • 0
    A

    I think is must be right, input [3,2,4], 6, output 2,3
    but the system show output 3,2 why????
    I test in my host, the output is 2,3

    class Solution {
    public:
    typedef struct
    {
    int number;
    int pos;
    }TEST;

    static bool SortByM1( const TEST &v1, const TEST &v2)
    {
        return v1.number < v2.number;
    }
    
    vector<int> twoSum(vector<int> &numbers, int target) {
        int i = 0, j = numbers.size()-1;
        vector <TEST> a;
    
        for(i=0; i<numbers.size(); i++)
        {
            TEST temp;
            temp.number = numbers[i];
            temp.pos = i;
            a.push_back(temp);
        }
    
        std::sort(a.begin(), a.end(), SortByM1);
    
        for(i=0; i<j;)
        {
            if(a[i].number + a[j].number == target)
            {
                int index1 = a[i].pos;
                int index2 = a[j].pos;
                if(index1 > index2)
                {
                    index1 = index2;
                    index2 = a[i].pos;
                }
                printf("index1=%d, index2=%d", index1+1, index2+1);
                break;
            }
            else if(a[i].number + a[j].number < target)
            {
                i++;
            }
            else
            {
                j--;
            }
        }
        return numbers;
    }
    

    };


  • 0
    A

    hoho, I'm sorry, I clear!!! The return value should be have result, The first problem is resolved!!

    class Solution {
    public:
    typedef struct
    {
    int number;
    int pos;
    }TEST;

    static bool SortByM1( const TEST &v1, const TEST &v2)
    {
        return v1.number < v2.number;
    }
    
    vector<int> twoSum(vector<int> &numbers, int target) {
        int i = 0, j = numbers.size()-1;
        vector <TEST> a;
    
        for(i=0; i<numbers.size(); i++)
        {
            TEST temp;
            temp.number = numbers[i];
            temp.pos = i;
            a.push_back(temp);
        }
    
        std::sort(a.begin(), a.end(), SortByM1);
        numbers.clear();
        
        for(i=0; i<j;)
        {
            if(a[i].number + a[j].number == target)
            {
                int index1 = a[i].pos;
                int index2 = a[j].pos;
                if(index1 > index2)
                {
                    index1 = index2;
                    index2 = a[i].pos;
                }
                printf("index1=%d, index2=%d ", index1+1, index2+1);
                numbers.push_back(index1+1);
                numbers.push_back(index2+1);
                break;
            }
            else if(a[i].number + a[j].number < target)
            {
                i++;
            }
            else
            {
                j--;
            }
        }
        
        return numbers;
    }
    

    };


Log in to reply
 

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