Skip to content

Commit 888871f

Browse files
authored
Create 001. Two Sum
1 parent a0e5599 commit 888871f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

001. Two Sum

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/****
2+
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
3+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
4+
e.g.
5+
Given nums = [2, 7, 11, 15], target = 9,
6+
Because nums[0] + nums[1] = 2 + 7 = 9,
7+
return [0, 1].
8+
****/
9+
10+
//C++ solution:
11+
class Solution {
12+
public:
13+
vector<int> twoSum(vector<int>& nums, int target) {
14+
15+
vector<int> result;
16+
int length = sizeof(nums);
17+
for(int i = 0; i < length; i++){
18+
for(int j = i+1; j <= length; j++){
19+
if (nums[i] + nums[j] == target) {
20+
//cout << "[" << i << "," << j << "]" << endl;
21+
result.push_back(i);
22+
result.push_back(j);
23+
return result;
24+
}
25+
}
26+
}
27+
cout << "no match\n";
28+
return result;
29+
}
30+
};

0 commit comments

Comments
 (0)