3 sum closest

Question

3 sum closest_第1张图片

Analysis

3 sum closest_第2张图片

Answer

class Solution{
public:
  vector > threeSum (vector  & num)
  {
    vector > result;
    if (num.size()<3) return result;
    int ans=0;
    sort (num.begin(),num.end());
    for (int i=0;i0 && num[i]==num[i-1])
        continue;
      int j=i+1;
      int k=num.size()-1;
      while(j=0 &&num[k]==num[k+1])
            --k;
         }
         else if (ans>0)
           --k;
         else
           ++j;
      }
    }
    return result;
  }
};

参考:https://www.kancloud.cn/kancloud/data-structure-and-algorithm-notes/72956

你可能感兴趣的:(3 sum closest)