首页 > 代码库 > 3 sum allow number used multi times

3 sum allow number used multi times

class Solution { public:     vector<vector<int> > ThreeSum(int a[], int n, int target)     {         vector<vector<int> > ret;         for (int i = 0; i < n; i++)         {             if(i>0 && a[i] == a[i - 1])                 continue;             int begin = i, end = n - 1;             while (begin <= end)             {                 int sum = a[i] + a[begin] + a[end];                 if (sum == target)                 {                     vector<int> res = { a[i], a[begin], a[end] };                     ret.push_back(res);                     begin++;                     end--;                     while (begin < end && a[begin] == a[begin - 1])                         begin++;                     while (begin<end && a[end] == a[end + 1])                         end--;                 }                 else if (sum>target)                 {                     end--;                 }                 else                 {                     begin++;                 }              }         }         return ret;     }};

 

3 sum allow number used multi times