here's my 3 sum code,
i don't get why my code slow down
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num)
{
vector<vector<int> > res;
sort(num.begin(), num.end());
for (int i = 0; i < num.size(); i++)
{
const int& a = num[i];
//if (a > 0) break; // save the loop times!
for (int j = i+1, k = num.size()-1; j < k; )
{
if (a+num[j]+num[k] < 0) j++;
else if (a+num[j]+num[k] >0) k--;
else
{
res.push_back({a,num[j],num[k]});
j++, k--;
while (j<k && num[j] == num[j-1]) j++;
while (j<k && num[k] == num[k+1]) k--;
}
}
while (i<num.size() && a == num[i+1]) i++;
}
return res;
}
};Uncomment the if line will double rumtime. (from 68ms to 102ms). I don't get why?