Skip to content
This repository was archived by the owner on May 16, 2021. It is now read-only.

Commit 0bf5cae

Browse files
committed
24 medium problems
1 parent adc6bd5 commit 0bf5cae

24 files changed

+1323
-0
lines changed

3sum-closest_1_AC.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <algorithm>
2+
#include <cmath>
3+
using std::abs;
4+
using std::sort;
5+
6+
class Solution {
7+
public:
8+
int threeSumClosest(vector<int>& nums, int target) {
9+
vector<int> &a = nums;
10+
int n = a.size();
11+
if (n < 3) {
12+
return 0;
13+
}
14+
sort(a.begin(), a.end());
15+
int res = a[0] + a[1] + a[2];
16+
int i, j, k;
17+
int val;
18+
for (i = 0; i < n - 2; ++i) {
19+
j = i + 1;
20+
k = n - 1;
21+
while (j < k) {
22+
val = a[i] + a[j] + a[k];
23+
if (abs(val - target) < abs(res - target)) {
24+
res = val;
25+
}
26+
if (val < target) {
27+
++j;
28+
} else if (val > target) {
29+
--k;
30+
} else {
31+
return target;
32+
}
33+
}
34+
}
35+
return res;
36+
}
37+
};

basic-calculator-ii_1_AC.cpp

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// So there's a way to hash put lambda function in a hash table after all.
2+
// That's a relief.
3+
#include <functional>
4+
#include <unordered_map>
5+
using std::function;
6+
using std::unordered_map;
7+
8+
class Solution {
9+
public:
10+
Solution() {
11+
fun["+"] = [](int x, int y) {return x + y;};
12+
fun["-"] = [](int x, int y) {return x - y;};
13+
fun["*"] = [](int x, int y) {return x * y;};
14+
fun["/"] = [](int x, int y) {return x / y;};
15+
16+
pre["("] = -1;
17+
int p = 0;
18+
pre["+"] = p;
19+
pre["-"] = p;
20+
++p;
21+
pre["*"] = p;
22+
pre["/"] = p;
23+
++p;
24+
}
25+
26+
int calculate(string s) {
27+
int ls = s.size();
28+
int res = 0;
29+
stack<int> nums;
30+
stack<string> ops;
31+
32+
int i = 0;
33+
string op;
34+
while (i < ls) {
35+
if (s[i] == '+' || s[i] == '-' || s[i] == '*' ||
36+
s[i] == '/') {
37+
op.clear();
38+
op.push_back(s[i++]);
39+
while (!ops.empty() && pre[ops.top()] >= pre[op]) {
40+
calc(nums, ops);
41+
}
42+
ops.push(op);
43+
} else if (s[i] == '(') {
44+
op.clear();
45+
op.push_back(s[i++]);
46+
ops.push(op);
47+
} else if (s[i] == ')') {
48+
while (ops.top() != "(") {
49+
calc(nums, ops);
50+
}
51+
ops.pop();
52+
++i;
53+
} else if (isdigit(s[i])) {
54+
int val = 0;
55+
while (i < ls && isdigit(s[i])) {
56+
val = val * 10 + (s[i] - '0');
57+
++i;
58+
}
59+
nums.push(val);
60+
} else {
61+
++i;
62+
}
63+
}
64+
while (!ops.empty()) {
65+
calc(nums, ops);
66+
}
67+
res = nums.top();
68+
nums.pop();
69+
return res;
70+
}
71+
72+
void calc(stack<int> &nums, stack<string> &ops) {
73+
if (nums.size() < 2 || ops.size() < 1) {
74+
return;
75+
}
76+
77+
string op = ops.top();
78+
ops.pop();
79+
int n2 = nums.top();
80+
nums.pop();
81+
int n1 = nums.top();
82+
nums.pop();
83+
nums.push(fun[op](n1, n2));
84+
}
85+
86+
~Solution() {
87+
fun.clear();
88+
pre.clear();
89+
}
90+
private:
91+
unordered_map<string, function<int(int, int)>> fun;
92+
unordered_map<string, int> pre;
93+
};

basic-calculator_1_AC.cpp

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <functional>
2+
#include <unordered_map>
3+
using std::function;
4+
using std::unordered_map;
5+
6+
class Solution {
7+
public:
8+
Solution() {
9+
fun["+"] = [](int x, int y) {return x + y;};
10+
fun["-"] = [](int x, int y) {return x - y;};
11+
fun["*"] = [](int x, int y) {return x * y;};
12+
fun["/"] = [](int x, int y) {return x / y;};
13+
14+
pre["("] = -1;
15+
int p = 0;
16+
pre["+"] = p;
17+
pre["-"] = p;
18+
++p;
19+
pre["*"] = p;
20+
pre["/"] = p;
21+
++p;
22+
}
23+
24+
int calculate(string s) {
25+
int ls = s.size();
26+
int res = 0;
27+
stack<int> nums;
28+
stack<string> ops;
29+
30+
int i = 0;
31+
string op;
32+
while (i < ls) {
33+
if (s[i] == '+' || s[i] == '-' || s[i] == '*' ||
34+
s[i] == '/') {
35+
op.clear();
36+
op.push_back(s[i++]);
37+
while (!ops.empty() && pre[ops.top()] >= pre[op]) {
38+
calc(nums, ops);
39+
}
40+
ops.push(op);
41+
} else if (s[i] == '(') {
42+
op.clear();
43+
op.push_back(s[i++]);
44+
ops.push(op);
45+
} else if (s[i] == ')') {
46+
while (ops.top() != "(") {
47+
calc(nums, ops);
48+
}
49+
ops.pop();
50+
++i;
51+
} else if (isdigit(s[i])) {
52+
int val = 0;
53+
while (i < ls && isdigit(s[i])) {
54+
val = val * 10 + (s[i] - '0');
55+
++i;
56+
}
57+
nums.push(val);
58+
} else {
59+
++i;
60+
}
61+
}
62+
while (!ops.empty()) {
63+
calc(nums, ops);
64+
}
65+
res = nums.top();
66+
nums.pop();
67+
return res;
68+
}
69+
70+
void calc(stack<int> &nums, stack<string> &ops) {
71+
if (nums.size() < 2 || ops.size() < 1) {
72+
return;
73+
}
74+
75+
string op = ops.top();
76+
ops.pop();
77+
int n2 = nums.top();
78+
nums.pop();
79+
int n1 = nums.top();
80+
nums.pop();
81+
nums.push(fun[op](n1, n2));
82+
}
83+
84+
~Solution() {
85+
fun.clear();
86+
pre.clear();
87+
}
88+
private:
89+
unordered_map<string, function<int(int, int)>> fun;
90+
unordered_map<string, int> pre;
91+
};

course-schedule_1_AC.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Topological sort using BFS.
2+
#include <queue>
3+
#include <vector>
4+
using std::queue;
5+
using std::vector;
6+
7+
class Solution {
8+
public:
9+
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
10+
int nv = numCourses;
11+
vector<vector<int>> g(nv);
12+
vector<int> ind(nv, 0);
13+
auto &e = prerequisites;
14+
queue<int> q;
15+
int x, y;
16+
int ne = e.size();
17+
18+
int i;
19+
for (i = 0; i < ne; ++i) {
20+
x = e[i].second;
21+
y = e[i].first;
22+
g[x].push_back(y);
23+
++ind[y];
24+
}
25+
26+
for (i = 0; i < nv; ++i) {
27+
if (ind[i] == 0) {
28+
q.push(i);
29+
}
30+
}
31+
32+
int ec = ne;
33+
while (!q.empty()) {
34+
x = q.front();
35+
q.pop();
36+
while (g[x].size() > 0) {
37+
y = g[x].back();
38+
g[x].pop_back();
39+
--ind[y];
40+
--ec;
41+
if (ind[y] == 0) {
42+
q.push(y);
43+
}
44+
}
45+
}
46+
return ec == 0;
47+
}
48+
};
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <queue>
2+
using std::priority_queue;
3+
4+
typedef struct Pair {
5+
vector<int> *a1, *a2;
6+
int i1, i2;
7+
8+
bool operator < (const Pair &p) const {
9+
return (*a1)[i1] + (*a2)[i2] < (*p.a1)[p.i1] + (*p.a2)[p.i2];
10+
}
11+
12+
bool operator > (const Pair &p) const {
13+
return (*a1)[i1] + (*a2)[i2] > (*p.a1)[p.i1] + (*p.a2)[p.i2];
14+
}
15+
16+
bool operator == (const Pair &p) const {
17+
return (*a1)[i1] + (*a2)[i2] == (*p.a1)[p.i1] + (*p.a2)[p.i2];
18+
}
19+
} Pair;
20+
21+
class Solution {
22+
public:
23+
vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
24+
vector<pair<int, int>> res;
25+
vector<int> &a1 = nums1;
26+
vector<int> &a2 = nums2;
27+
int n1 = a1.size();
28+
int n2 = a2.size();
29+
if (n1 == 0 || n2 == 0) {
30+
return res;
31+
}
32+
33+
priority_queue<Pair, vector<Pair>, greater<Pair>> pq;
34+
int i;
35+
Pair p;
36+
37+
p.a1 = &a1;
38+
p.a2 = &a2;
39+
for (i = 0; i < n1; ++i) {
40+
p.i1 = i;
41+
p.i2 = 0;
42+
pq.push(p);
43+
}
44+
for (i = 0; i < k && !pq.empty(); ++i) {
45+
p = pq.top();
46+
pq.pop();
47+
res.push_back(make_pair((*p.a1)[p.i1], (*p.a2)[p.i2]));
48+
++p.i2;
49+
if (p.i2 < (*p.a2).size()) {
50+
pq.push(p);
51+
}
52+
}
53+
while (!pq.empty()) {
54+
pq.pop();
55+
}
56+
return res;
57+
}
58+
};

gas-station_1_AC.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
4+
int n = gas.size();
5+
if (n == 0) {
6+
return true;
7+
}
8+
int sum = 0;
9+
int i, j;
10+
11+
j = 0;
12+
for (i = 0; i < 2 * n - 1; ++i) {
13+
if (i - j == n) {
14+
break;
15+
}
16+
sum += gas[i % n] - cost[i % n];
17+
if (sum < 0) {
18+
j = i + 1;
19+
sum = 0;
20+
}
21+
}
22+
return i - j == n ? j : -1;
23+
}
24+
};

0 commit comments

Comments
 (0)