일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Class
- t1
- 43. Multiply Strings
- 운영체제
- Python Implementation
- Regular Expression
- Python Code
- Decorator
- 시바견
- attribute
- data science
- Substring with Concatenation of All Words
- shiba
- 715. Range Module
- Generator
- 프로그래머스
- 컴퓨터의 구조
- 315. Count of Smaller Numbers After Self
- LeetCode
- DWG
- 밴픽
- 파이썬
- 30. Substring with Concatenation of All Words
- concurrency
- iterator
- Convert Sorted List to Binary Search Tree
- Python
- kaggle
- 109. Convert Sorted List to Binary Search Tree
- Protocol
Archives
- Today
- Total
Scribbling
[C++ Reminder] LeetCode: 15. 3Sum 본문
#include <vector>
#include <algorithm>
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
using namespace std;
sort(nums.begin(), nums.end());
vector<vector<int>> ret;
for (size_t i=0; i<nums.size()-2; i++) {
if (i > 0 and nums[i] == nums[i-1]) {
continue;
}
size_t j = i + 1, k = nums.size() - 1;
while (j < k) {
int num1 = nums.at(i), num2 = nums.at(j), num3 = nums.at(k);
int summed = num1 + num2 + num3;
if (summed == 0) {
vector<int> temp = {num1, num2, num3};
ret.push_back(temp);
while (j < k and nums[j] == num2) {
j += 1;
}
while (j < k and nums[k] == num3) {
k -= 1;
}
} else if (summed < 0) {
j += 1;
} else {
k -= 1;
}
}
}
return ret;
}
};
'Computer Science > C++' 카테고리의 다른 글
[C++] 49. Group Anagrams (0) | 2023.08.18 |
---|---|
[C++] LeetCode 242. Valid Anagram (0) | 2023.08.18 |
[C++] LeetCode 217. Contains Duplicate (0) | 2023.08.18 |
[C++ Reminder] LeetCode 2. Add Two Numbers (0) | 2023.01.23 |
[C++ Reminder] LeetCode 1. Two Sum (0) | 2023.01.23 |