You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include<vector>
#include<algorithm>classSolution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;
int n = nums.size();
sort(nums.begin(), nums.end());
for (int i = 0; i < n - 2; i++){
if (i > 0 && nums[i] == nums[i - 1])
continue;
int left = i + 1;
int right = n - 1;
while (left < right){
int sum = nums[i] + nums[left] + nums[right];
if (sum == 0){
result.push_back({nums[i], nums[left], nums[right]});
while (left < right && nums[left] == nums[left + 1])
left++;
while (left < right && nums[right] == nums[right - 1])
right--;
left++;
right--;
}
elseif (sum < 0)
left++;
else
right--;
}
}
return result;
}
};
간단할 줄 알았는데, 여러운 문제... LeetCode에서 힌트를 볼 수 있었따니.. 이제 알았다.
중복 제거가 핵심이고, 3개이기 때문에 투포인터라 푼다.
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: