-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo_sum_solution.cpp
More file actions
40 lines (28 loc) · 1.08 KB
/
two_sum_solution.cpp
File metadata and controls
40 lines (28 loc) · 1.08 KB
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
32
33
34
35
36
37
38
/*
Daniel Diaz
LeetCode Problem - Two Sum
- Description:
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
*/
// - My Solution:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> v;
//outer loop index's through each element within vector
for (int currIndex = 0; (int) currIndex < nums.size(); ++currIndex) {
//inner loop index's starting one past currIndex of outerloop
for (int i = currIndex + 1; (int) i < nums.size(); ++i) {
if (nums[i] + nums[currIndex] == target) {
v.push_back(currIndex);
v.push_back(i);
return v;
}
}
}
//if no pair found, will return empty vector
return v;
}
};