-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoSum_leetcode001.cpp
210 lines (180 loc) · 4.78 KB
/
TwoSum_leetcode001.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
#include <iostream>
#include <vector>
#include <unordered_map>
#include <time.h>
#include <chrono>
using namespace std::chrono;
using namespace std;
/*
Time complexity : O(n^2). For each element, we try to find its complement
by looping through the rest of array which takes O(n)O(n) time.
Therefore, the time complexity is O(n^2)
Space complexity : O(1).
*/
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
for (int i = 0; i < nums.size(); ++i)
{
int temp = target - nums[i];
for (int k = 0; k < nums.size(); ++k)
{
if ((temp == nums[k]) && (k != i))
{
//result.push_back(i);
//result.push_back(k);
result[0] = i;
result[1] = k;
return result;
}
}
}
return result;
}
Solution():result(2,-1) //类成员对象初始化必须使用初始化列表
{
//result.assign(2,-1);
}
private:
int a = 1;
vector<int> result;
};
/*
Complexity Analysis:
Time complexity : O(n). We traverse the list containing nn elements exactly twice.
Since the hash table reduces the look up time to O(1), the time complexity is O(n).
Space complexity : O(n). The extra space required depends on the number of items stored in the hash table,
which stores exactly nn elements.
*/
class Solution1 {
public:
vector<int> twoSum(vector<int>& nums, int target) {
// hash[i]表示nums中数值为i的下标
unordered_map<int, int> hash;
vector<int> result;
// 一边循环每个数,一边加入hash表。
for (int i = 0; i < nums.size(); i++) {
if (hash.find(target - nums[i]) != hash.end()) {
// target - nums[i]的下标更小,放在前面
result.push_back(hash[target - nums[i]]);
result.push_back(i);
return result;
}
hash[nums[i]] = i;
}
// 无解的情况
result.push_back(-1);
result.push_back(-1);
return result;
}
};
class Solution2 {
public:
int findY(int x, int target){
if(x == 0) return target;
if(target == 0) return -x;
else{
if(x == target)
return 0;
else
return -x + target;
}
}
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
unordered_map<int, int> bank;
int find = 0;
for(int i = 0; i < nums.size(); i++){
find = findY(nums[i], target);
if(bank.find(find) != bank.end()){
result.push_back(i);
result.push_back(bank[find]);
return result;
}else{
if(bank.find(nums[i]) == bank.end())
bank.insert({nums[i],i});
}
}
return result;
}
};
/*
Solution mys;
0.003831
Running time is: 0.118ms
result[0] is 1
result[1] is 2
9个元素
0.004282
Running time is: 0.107ms
result[0] is 1
result[1] is 7
Solution1 mys;
0.021745
Running time is: 0.141ms
result[0] is 1
result[1] is 2
9个元素
0.040912
Running time is: 0.142ms
result[0] is 1
result[1] is 7
为什么hash时间更长
以下是50万个元素的测试数据
Solution2 mys;
250.164
Running time is: 252.498ms
result[0] is 499996
result[1] is 499994
Solution1 mys;
226.121
Running time is: 228.478ms
result[0] is 499994
result[1] is 499996
^C
njl@njl-HP:~/Documents/test$ ./a.out
226.451
Running time is: 229.755ms
result[0] is 499994
result[1] is 499996
Solution mys;
897492 竟然需要大概15分钟
Running time is: 897455ms
result[0] is 499992
result[1] is 499998
*/
int main(int argc, char * * argv)
{
int test[500000] = {0};
for(int i =0;i<500000;i++)
{
test[i] = i;
//cout<<i<<endl;
}
vector<int> result1(2, -1);
vector<int> nums(test, test + 499999);
int target = 999990;
Solution1 mys;
vector<int> result;
clock_t start_time = clock();
high_resolution_clock::time_point t1 = high_resolution_clock::now(); //返回时间戳
result = mys.twoSum(nums, target);
high_resolution_clock::time_point t2 = high_resolution_clock::now();
duration<double, std::milli> time_span = t2 - t1;
std::cout << time_span.count() << std::endl;
clock_t end_time = clock();
cout << "Running time is: " << static_cast<double>(end_time - start_time) / CLOCKS_PER_SEC * 1000 << "ms" << endl;//输出运行时间
for (int i = 0; i < result.size(); ++i)
{
cout << "result[" << i << "] is " << result[i] << endl;
}
cin >> target;
}