my solution


  • 0

    vector<int> twoSum(vector<int>& nums, int target) {
    vector<int> res;
    int sz = nums.size();
    int mx = -2147483648, mn = 2147483647;
    for(int i = 0; i < sz; i++){
    if(nums[i] > mx) mx = nums[i];
    if(nums[i] < mn) mn = nums[i];
    }
    int state = (int) calloc((mx-mn+1),sizeof(int));
    for(int i = 0; i < sz; i++){
    state[nums[i] - mn] = i+1;
    }
    for(int i = 0; i < sz; i++){
    int nx = target - nums[i];
    if(nx <= mx && nx >= mn && state[nx-mn] != 0){
    int nxidx = state[nx-mn] - 1;
    if(i!= nxidx){
    res.push_back(i);
    res.push_back(nxidx);
    free(state);
    return res;
    }
    }
    }
    free(state);
    return res;
    }


Log in to reply
 

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