Skip to content

Commit 469c1ca

Browse files
committed
change tab to 4 space
1 parent dc35596 commit 469c1ca

File tree

160 files changed

+3861
-3861
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+3861
-3861
lines changed

solutions/001.Two_Sum/AC_hash_array_n.cpp

+33-33
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* File: ac_hash_array_n.cpp
44
* Create Date: 2014-11-26 10:54:11
55
* Descripton: use array to save the status,
6-
* but if the MAXN is too large, it will Exeed Memory Limit
7-
* time:n, memory: MAXN
6+
* but if the MAXN is too large, it will Exeed Memory Limit
7+
* time:n, memory: MAXN
88
*/
99

1010
#include <bits/stdc++.h>
@@ -15,40 +15,40 @@ const int N = 200002;
1515
const int OFFSET = 100000;
1616

1717
class Solution {
18-
private:
19-
int idOfNum[N];
20-
public:
21-
vector<int> twoSum(vector<int> &numbers, int target) {
22-
vector<int> index;
23-
memset(idOfNum, 0, sizeof(idOfNum));
18+
private:
19+
int idOfNum[N];
20+
public:
21+
vector<int> twoSum(vector<int> &numbers, int target) {
22+
vector<int> index;
23+
memset(idOfNum, 0, sizeof(idOfNum));
2424

25-
int sz = numbers.size();
26-
for (int i = 0; i < sz; i++) {
27-
int rest = target - numbers[i];
28-
if (idOfNum[rest + OFFSET]) {
29-
index.push_back(idOfNum[rest + OFFSET]);
30-
index.push_back(i + 1);
31-
return index;
32-
}
33-
idOfNum[numbers[i] + OFFSET] = i + 1;
34-
}
35-
// Program never go here, because
36-
// "each input would have exactly one solution"
37-
}
25+
int sz = numbers.size();
26+
for (int i = 0; i < sz; i++) {
27+
int rest = target - numbers[i];
28+
if (idOfNum[rest + OFFSET]) {
29+
index.push_back(idOfNum[rest + OFFSET]);
30+
index.push_back(i + 1);
31+
return index;
32+
}
33+
idOfNum[numbers[i] + OFFSET] = i + 1;
34+
}
35+
// Program never go here, because
36+
// "each input would have exactly one solution"
37+
}
3838
};
3939

4040
int main() {
41-
Solution s;
42-
vector<int> in;
43-
int n, t;
44-
cin >> n;
45-
while (n--) {
46-
cin >> t;
47-
in.push_back(t);
48-
}
49-
cin >> t;
50-
in = s.twoSum(in, t);
51-
cout << in[0] << ' ' << in[1] << endl;
52-
return 0;
41+
Solution s;
42+
vector<int> in;
43+
int n, t;
44+
cin >> n;
45+
while (n--) {
46+
cin >> t;
47+
in.push_back(t);
48+
}
49+
cin >> t;
50+
in = s.twoSum(in, t);
51+
cout << in[0] << ' ' << in[1] << endl;
52+
return 0;
5353
}
5454

solutions/001.Two_Sum/AC_map_hash_nlogn.cpp

+32-32
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* File: ac_hash_map_nlogn.cpp
44
* Create Date: 2014-11-26 11:09:16
55
* Descripton: Save with map.
6-
* time:nlogn, memory:nlogn
6+
* time:nlogn, memory:nlogn
77
*/
88

99
#include <bits/stdc++.h>
@@ -12,41 +12,41 @@ typedef long long ll;
1212
using namespace std;
1313

1414
class Solution {
15-
private:
16-
map<int, int> idOfNum;
17-
public:
18-
vector<int> twoSum(vector<int> &numbers, int target) {
19-
vector<int> index;
20-
idOfNum.clear();
15+
private:
16+
map<int, int> idOfNum;
17+
public:
18+
vector<int> twoSum(vector<int> &numbers, int target) {
19+
vector<int> index;
20+
idOfNum.clear();
2121

22-
int sz = numbers.size();
23-
for (int i = 0; i < sz; i++) {
24-
int rest = target - numbers[i];
25-
if (idOfNum.find(rest) != idOfNum.end()) {
26-
index.push_back(idOfNum[rest]);
27-
index.push_back(i + 1);
28-
return index;
29-
}
30-
idOfNum[numbers[i]] = i + 1;
31-
}
32-
// Program never go here, because
33-
// "each input would have exactly one solution"
34-
}
22+
int sz = numbers.size();
23+
for (int i = 0; i < sz; i++) {
24+
int rest = target - numbers[i];
25+
if (idOfNum.find(rest) != idOfNum.end()) {
26+
index.push_back(idOfNum[rest]);
27+
index.push_back(i + 1);
28+
return index;
29+
}
30+
idOfNum[numbers[i]] = i + 1;
31+
}
32+
// Program never go here, because
33+
// "each input would have exactly one solution"
34+
}
3535
};
3636

3737
int main() {
38-
Solution s;
39-
vector<int> in;
40-
int n, t;
41-
cin >> n;
42-
while (n--) {
43-
cin >> t;
44-
in.push_back(t);
45-
}
46-
cin >> t;
47-
in = s.twoSum(in, t);
48-
cout << in[0] << ' ' << in[1] << endl;
49-
return 0;
38+
Solution s;
39+
vector<int> in;
40+
int n, t;
41+
cin >> n;
42+
while (n--) {
43+
cin >> t;
44+
in.push_back(t);
45+
}
46+
cin >> t;
47+
in = s.twoSum(in, t);
48+
cout << in[0] << ' ' << in[1] << endl;
49+
return 0;
5050
}
5151

5252

solutions/001.Two_Sum/AC_two_points_nlogn.cpp

+40-40
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,50 @@ using namespace std;
1212
const int N = 0;
1313

1414
class Solution {
15-
public:
16-
vector<int> twoSum(vector<int> &numbers, int target) {
17-
int sz = numbers.size();
18-
int left = 0, right = sz - 1, sum = 0;
15+
public:
16+
vector<int> twoSum(vector<int> &numbers, int target) {
17+
int sz = numbers.size();
18+
int left = 0, right = sz - 1, sum = 0;
1919

20-
vector<int> sorted (numbers);
21-
std::sort(sorted.begin(), sorted.end());
20+
vector<int> sorted (numbers);
21+
std::sort(sorted.begin(), sorted.end());
2222

23-
vector<int> index;
24-
while (left < right) {
25-
sum = sorted[left] + sorted[right];
26-
if (sum == target) {
27-
// find the answer
28-
for (int i = 0; i < sz; i++) {
29-
if (numbers[i] == sorted[left])
30-
index.push_back(i + 1);
31-
else if (numbers[i] == sorted[right])
32-
index.push_back(i + 1);
33-
if (index.size() == 2)
34-
return index;
35-
}
36-
} else if (sum > target) {
37-
right--;
38-
} else {
39-
left++;
40-
}
41-
}
42-
// Program never go here, because
43-
// "each input would have exactly one solution"
44-
}
23+
vector<int> index;
24+
while (left < right) {
25+
sum = sorted[left] + sorted[right];
26+
if (sum == target) {
27+
// find the answer
28+
for (int i = 0; i < sz; i++) {
29+
if (numbers[i] == sorted[left])
30+
index.push_back(i + 1);
31+
else if (numbers[i] == sorted[right])
32+
index.push_back(i + 1);
33+
if (index.size() == 2)
34+
return index;
35+
}
36+
} else if (sum > target) {
37+
right--;
38+
} else {
39+
left++;
40+
}
41+
}
42+
// Program never go here, because
43+
// "each input would have exactly one solution"
44+
}
4545
};
4646

4747
int main() {
48-
Solution s;
49-
vector<int> in;
50-
int n, t;
51-
cin >> n;
52-
while (n--) {
53-
cin >> t;
54-
in.push_back(t);
55-
}
56-
cin >> t;
57-
in = s.twoSum(in, t);
58-
cout << in[0] << ' ' << in[1] << endl;
59-
return 0;
48+
Solution s;
49+
vector<int> in;
50+
int n, t;
51+
cin >> n;
52+
while (n--) {
53+
cin >> t;
54+
in.push_back(t);
55+
}
56+
cin >> t;
57+
in = s.twoSum(in, t);
58+
cout << in[0] << ' ' << in[1] << endl;
59+
return 0;
6060
}
6161

solutions/001.Two_Sum/TLE_bruteforce_n2.cpp

+26-26
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,34 @@ using namespace std;
1212
const int N = 0;
1313

1414
class Solution {
15-
public:
16-
vector<int> twoSum(vector<int> &numbers, int target) {
17-
int sz = numbers.size();
18-
vector<int> index;
19-
for (int i = 0; i < sz; i++)
20-
for (int j = i + 1; j < sz; j++)
21-
if (numbers[i] + numbers[j] == target) {
22-
index.push_back(i + 1);
23-
index.push_back(j + 1);
24-
return index;
25-
}
26-
// Program never go here, because
27-
// "each input would have exactly one solution"
28-
}
15+
public:
16+
vector<int> twoSum(vector<int> &numbers, int target) {
17+
int sz = numbers.size();
18+
vector<int> index;
19+
for (int i = 0; i < sz; i++)
20+
for (int j = i + 1; j < sz; j++)
21+
if (numbers[i] + numbers[j] == target) {
22+
index.push_back(i + 1);
23+
index.push_back(j + 1);
24+
return index;
25+
}
26+
// Program never go here, because
27+
// "each input would have exactly one solution"
28+
}
2929
};
3030

3131
int main() {
32-
Solution s;
33-
vector<int> in;
34-
int n, t;
35-
cin >> n;
36-
while (n--) {
37-
cin >> t;
38-
in.push_back(t);
39-
}
40-
cin >> t;
41-
in = s.twoSum(in, t);
42-
cout << in[0] << ' ' << in[1] << endl;
43-
return 0;
32+
Solution s;
33+
vector<int> in;
34+
int n, t;
35+
cin >> n;
36+
while (n--) {
37+
cin >> t;
38+
in.push_back(t);
39+
}
40+
cin >> t;
41+
in = s.twoSum(in, t);
42+
cout << in[0] << ' ' << in[1] << endl;
43+
return 0;
4444
}
4545

0 commit comments

Comments
 (0)