Skip to content

Commit e28cbd5

Browse files
committed
basic_code
1 parent 56d5877 commit e28cbd5

File tree

7 files changed

+166
-20
lines changed

7 files changed

+166
-20
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
.idea
44
.history
5-
.vscode
5+
.vscode
6+
**.o

cpp/basic/main.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
#include "sort.h"
3+
4+
int main(int argc, const char **argv) {
5+
basic::Sort s;
6+
std::vector<int> arr = {3, 8, 9, 1, 8, 3, 4, 2};
7+
basic::PrintVector("origin: ", arr);
8+
s.QuickSort(arr);
9+
basic::PrintVector("sorted: ", arr);
10+
}

cpp/basic/sort.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "util.h"
2+
#include <vector>
3+
namespace basic {
4+
5+
class Sort {
6+
private:
7+
void quickSortImp(std::vector<int> &arr, int left, int right) {
8+
if (left >= right) {
9+
return;
10+
}
11+
int datum = arr[left];
12+
int i = left, j = right;
13+
while (i < j) { // 每次循环交换两个数
14+
// 因为arr[left]已经缓存到datum,先找右侧比基准小的,直接将arr[left]换掉
15+
while (i < j && arr[j] >= datum) {
16+
j--;
17+
}
18+
arr[i] = arr[j];
19+
while (i < j && arr[i] < datum) {
20+
i++;
21+
}
22+
arr[j] = arr[i];
23+
}
24+
arr[i] = datum;
25+
quickSortImp(arr, left, i - 1);
26+
quickSortImp(arr, i + 1, right);
27+
}
28+
29+
public:
30+
void QuickSort(std::vector<int> &arr) {
31+
return quickSortImp(arr, 0, arr.size() - 1);
32+
}
33+
};
34+
35+
} // namespace basic

cpp/basic/util.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
#include <iostream>
3+
#include <string>
4+
#include <vector>
5+
namespace basic {
6+
static void PrintVector(const std::string &msg, const std::vector<int> &input) {
7+
std::cout << msg << "[";
8+
for (const auto &item : input) {
9+
std::cout << item << ",";
10+
}
11+
std::cout << "]" << std::endl;
12+
}
13+
} // namespace basic

cpp/concurrent/blocking_queue.h

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
#include <array>
3+
#include <condition_variable>
4+
#include <cstdint>
5+
#include <mutex>
6+
#include <utility>
7+
namespace concurrent {
8+
template <typename T, std::size_t cap> class BlockingQueue {
9+
public:
10+
BlockingQueue(){};
11+
12+
~BlockingQueue(){};
13+
14+
// block while full
15+
void push(const T &val) {
16+
std::unique_lock<std::mutex> lock(mutex_);
17+
producer_cond_.wait(lock, [this] { return w_idx_ - r_idx_ < cap; });
18+
data_[w_idx_ % cap] = val;
19+
w_idx_++;
20+
consumer_cond_.notify_one();
21+
};
22+
23+
// block while empty
24+
T pop() {
25+
std::unique_lock<std::mutex> lock(mutex_);
26+
consumer_cond_.wait(lock, [this] { return w_idx_ > r_idx_; });
27+
auto idx = r_idx_ % cap;
28+
r_idx_++;
29+
producer_cond_.notify_one();
30+
return data_[idx];
31+
};
32+
33+
int size() {
34+
std::lock_guard<std::mutex> lock(mutex_);
35+
return w_idx_ - r_idx_;
36+
}
37+
38+
private:
39+
std::array<T, cap> data_;
40+
std::int64_t r_idx_ = 0;
41+
std::int64_t w_idx_ = 0;
42+
std::mutex mutex_;
43+
std::condition_variable producer_cond_;
44+
std::condition_variable consumer_cond_;
45+
};
46+
} // namespace concurrent

cpp/concurrent/main.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
#include "blocking_queue.h"
3+
#include <chrono>
4+
#include <cstdio>
5+
#include <iostream>
6+
#include <thread>
7+
8+
int main() {
9+
constexpr int num = 2;
10+
std::thread consumers[num], producers[num];
11+
concurrent::BlockingQueue<std::int32_t, 5> bq;
12+
for (int i = 0; i < num; ++i) {
13+
consumers[i] = std::thread([i, &bq] {
14+
int n = bq.pop();
15+
while (n > 0) {
16+
std::printf("---- cc (%d) ---- : %d \n", i, n);
17+
n = bq.pop();
18+
}
19+
});
20+
producers[i] = std::thread([i, &bq] {
21+
int n = 1;
22+
while (n < 10) {
23+
std::printf("---- pp (%d) ---- : %d \n", i, i * 100 + n);
24+
bq.push(i * 100 + n);
25+
n++;
26+
std::this_thread::sleep_for(std::chrono::milliseconds(500));
27+
}
28+
bq.push(-1);
29+
});
30+
}
31+
32+
// join them back:
33+
for (int i = 0; i < num; ++i) {
34+
producers[i].join();
35+
consumers[i].join();
36+
}
37+
38+
return 0;
39+
}

cpp/leetcode/407.接雨水-ii.cpp

+21-19
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,27 @@
5353
*
5454
*/
5555

56+
#include <algorithm>
57+
#include <array>
58+
#include <functional>
5659
#include <queue>
60+
#include <utility>
5761
#include <vector>
58-
5962
// @lc code=start
6063
class Solution {
6164
using pii = std::pair<int, int>;
6265

63-
public:
66+
public:
6467
int trapRainWater(std::vector<std::vector<int>> &heightMap) {
68+
int res = 0;
6569
if (heightMap.size() < 3 || heightMap[0].size() < 3) {
66-
return 0;
70+
return res;
6771
}
68-
std::priority_queue<pii, std::vector<pii>, std::greater<pii>> q;
6972
int m = heightMap.size();
7073
int n = heightMap[0].size();
7174
std::vector<bool> visited(m * n, false);
75+
std::priority_queue<pii, std::vector<pii>, std::greater<pii>> q;
76+
7277
// check edge
7378
for (int i = 0; i < m; i++) {
7479
for (int j = 0; j < n; j++) {
@@ -80,23 +85,20 @@ class Solution {
8085
}
8186
}
8287

83-
// check all block
84-
int res = 0;
85-
int dir[] = {-1, 0, 1, 0, -1}; // left/bottom/right/top
88+
int dirs[] = {-1, 0, 1, 0, -1};
89+
// check every grid
8690
while (!q.empty()) {
87-
pii cur = q.top();
91+
auto cur = q.top();
8892
q.pop();
89-
for (int k = 0; k < 4; k++) {
90-
int x = cur.second / n + dir[k];
91-
int y = cur.second % n + dir[k + 1];
92-
int xyi = x * n + y;
93-
if (x >= 0 && x < m && y >= 0 && y < n &&
94-
!visited[xyi]) { // not edge
95-
if (heightMap[x][y] < cur.first) {
96-
res += cur.first - heightMap[x][y];
97-
}
98-
visited[xyi] = true;
99-
q.push({std::max(heightMap[x][y], cur.first), xyi});
93+
94+
for (int k = 0; k < 4; k++) { // check left/down/right/up
95+
int x = cur.second / n + dirs[k];
96+
int y = cur.second % n + dirs[k + 1];
97+
int idx = x * n + y;
98+
if (x >= 0 && x < m && y >= 0 && y < n && !visited[idx]) {
99+
res += cur.first > heightMap[x][y] ? cur.first - heightMap[x][y] : 0;
100+
visited[idx] = true;
101+
q.push({std::max(heightMap[x][y], cur.first), idx});
100102
}
101103
}
102104
}

0 commit comments

Comments
 (0)