-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeetCode_Q1_TwoSum.cpp
54 lines (51 loc) · 1.35 KB
/
LeetCode_Q1_TwoSum.cpp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> nums{11, 15, 7, 2};
int target = 9;
bool answer = false;
int index2 = nums.size();
int index1;
while (!answer && !nums.empty()){
int num1 = nums.back();
int good = target - num1;
nums.pop_back();
index1 = 0;
for (vector<int>::iterator itr = nums.begin(); itr != nums.end(); ++itr){
if (*itr == good){
answer = true;
break;
}
++index1;
}
--index2;
}
cout << index1 << ' ' << index2;
}
/*
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
bool answer = false;
int index2 = nums.size();
int index1;
while (!answer && !nums.empty()){
int num1 = nums.back();
int good = target - num1;
nums.pop_back();
index1 = 0;
for (vector<int>::iterator itr = nums.begin(); itr != nums.end(); ++itr){
if (*itr == good){
answer = true;
break;
}
++index1;
}
--index2;
}
vector<int> indices{index1,index2};
return indices;
}
};
*/