일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- LeetCode
- iterator
- Python Code
- 109. Convert Sorted List to Binary Search Tree
- Decorator
- Substring with Concatenation of All Words
- concurrency
- 파이썬
- 43. Multiply Strings
- Python Implementation
- t1
- 컴퓨터의 구조
- 315. Count of Smaller Numbers After Self
- 운영체제
- 715. Range Module
- kaggle
- Class
- Regular Expression
- 프로그래머스
- Python
- attribute
- data science
- Convert Sorted List to Binary Search Tree
- shiba
- 시바견
- 밴픽
- Protocol
- 30. Substring with Concatenation of All Words
- DWG
- Generator
Archives
- Today
- Total
Scribbling
[C++ Reminder] LeetCode 1. Two Sum 본문
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> res;
unordered_map<int, int> hash;
for (int i = 0; i < nums.size(); i++) {
int num = target - nums[i];
// found
if (hash.find(num) != hash.end()) {
res.push_back(hash[num]);
res.push_back(i);
return res;
}
hash[nums[i]] = i;
}
return res;
}
};
int main() {
vector<int> vec{ 2, 7, 11, 15 };
Solution().twoSum(vec, 9);
return 0;
}
'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: 15. 3Sum (0) | 2023.01.26 |
[C++ Reminder] LeetCode 2. Add Two Numbers (0) | 2023.01.23 |