Need help for a unique problem

  • 0

    Here is my solution, when submitting it there is a failed case

    The problem is when I run this case in the Run Code mode, the answer is right but it failed when I tried to submit it. It's quite weird, any suggestions? Thanks.

    class Solution {
    int FindUpper(vector<int>&nums,int l,int target){
    int r = nums.size();
    int mid = l+((r-l)>>1);
    l = mid+1;
    else r = mid;
    return r;
    int threeSumClosest(vector<int>& nums, int target) {
    int m =nums.size();
    // int closest = nums[m-1]+nums[m-2]+nums[m-3];
    int diff = INT_MAX,closest=0;
    for(int i=0;i<nums.size()-2;++i){
    for(int j=i+1;j<nums.size()-1;++j){
    int tmp = target-nums[i]-nums[j];
    int l = j+1,lower,upper;
    int idx = FindUpper(nums,l,tmp);
    lower = nums[idx-1];
    upper = nums[idx];
    lower = nums[idx];
    upper = nums[idx];
    int smaller = (abs(tmp-lower)<abs(tmp-upper))?lower:upper;
    if (abs(tmp-smaller)<diff){
    diff = abs(tmp-smaller);
    closest = smaller+nums[i]+nums[j];
    cout<<"diff "<<diff<<"; closest "<<closest<<endl;
    return closest;

Log in to reply

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