-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.cpp
More file actions
executable file
·43 lines (36 loc) · 999 Bytes
/
TwoSum.cpp
File metadata and controls
executable file
·43 lines (36 loc) · 999 Bytes
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
//
// TwoSum.cpp
// leetcode
//
// Created by witwolf on 10/24/14.
// Copyright (c) 2014 witwolf. All rights reserved.
//
#include <vector>
#include <iostream>
using namespace std;
vector<int> twoSum(vector<int> &numbers, int target) {
int i = 0 , j = (int) numbers.size() - 1;
while(i < j ){
if(numbers[i] + numbers[j] < target){
i++ ;
}else if(numbers[i] + numbers[j] > target){
j--;
}else{
break;
}
}
vector<int> ret;
ret.push_back(i);
ret.push_back(j);
return ret;
}
int main(int argc,char **argv){
int data[] = {2,7,11,15,19,80};
vector<int> numbers(data,data + sizeof(data)/sizeof(int));
int target = 9 ;
vector<int> ret = twoSum(numbers, target);
std::cout << "index1=" << ret[0] << ", index2=" << ret[1] << std::endl;
target = 30 ;
ret = twoSum(numbers, target);
std::cout << "index1=" << ret[0] << ", index2=" << ret[1] << std::endl;
}