-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_TwoSum.cpp
102 lines (96 loc) · 2.2 KB
/
1_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
O(n^2)
Runtime: 106 ms.
Your runtime beats 34.93% of cpp submissions
2017/2/22
*/
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int>a(2,0);
for(int i=0;i<nums.size()-1;i++)
for(int j=i+1;j<nums.size();j++)
if (nums[i]+nums[j]==target){
a[0]=i;
a[1]=j;
return a;
}
}
};
/*
O(nlogn)
Runtime: 6 ms.
Your runtime beats 94.18% of cpp submissions
2017/2/22
*/
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> pri_nums(nums.begin(),nums.end());
sort(nums.begin(),nums.end());
vector<int>a(2,0);
bool flag=false;
for(int i=0;i<nums.size()-1;i++){
int low=i+1,high=nums.size()-1,middle;
if(flag){
break;
}
while(low<=high){
middle=(low+high)/2;
if (nums[i]+nums[middle]==target){
a[0]=nums[i];
a[1]=nums[middle];
flag=true;
break;
}else if(nums[i]+nums[middle]<target){
low=middle+1;
} else {
high=middle-1;
}
}
}
for(int i=0;i<pri_nums.size();i++){
if(a[0]==pri_nums[i]) {
a[0]=i;
break;
}
}
for(int i=0;i<pri_nums.size();i++){
if(a[1]==pri_nums[i]&& i!=a[0]) {
a[1]=i;
break;
}
}
if(a[0]>a[1])
swap(a[0],a[1]);
return a;
}
};
/*
O(n)??? 不清楚 map.count的复杂度,怎么会这么慢呢??网上看到同样的算法,用了 unordered_map是6ms
Runtime: 9 ms.
Your runtime beats 68.32% of cpp submissions
2017/2/22
*/
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int>a(2,0);
map<int,int>M;
M.clear();
int len=nums.size();
for(int i=0;i<len;i++)
{
if(M.count(nums[i])==0)
{
M[nums[i]]=i;
}
if(M.count(target-nums[i])==1 && M[target-nums[i]]!=i)
{
a[0]=M[target-nums[i]];
a[1]=i;
return a;
}
}
}
};