Skip to content

Commit 2002c8b

Browse files
committed
443,652
1 parent a327113 commit 2002c8b

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

algorithms/p443/443.hpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
#ifndef LEETCODE_443_HPP
3+
#define LEETCODE_443_HPP
4+
5+
#include <iostream>
6+
#include <queue>
7+
#include <algorithm>
8+
#include <vector>
9+
#include <unordered_map>
10+
#include <unordered_set>
11+
#include <set>
12+
#include <numeric>
13+
#include <stack>
14+
#include <string>
15+
16+
using namespace std;
17+
18+
19+
class Solution {
20+
public:
21+
int compress(vector<char> &chars) {
22+
int cnt = 1, cur = 0;
23+
char pre = 0;
24+
for (int i = 0; i < chars.size(); ++i) {
25+
if (chars[i] == pre) {
26+
cnt++;
27+
} else {
28+
cur = writeOneChar(chars, pre, cnt, cur);
29+
pre = chars[i];
30+
cnt = 1;
31+
}
32+
}
33+
cur = writeOneChar(chars, pre, cnt, cur);
34+
return cur;
35+
36+
}
37+
38+
int writeOneChar(vector<char> &chars, char c, int cnt, int cur) {
39+
if (c == 0)
40+
return 0;
41+
chars[cur++] = c;
42+
if (cnt > 1) {
43+
string count = to_string(cnt);
44+
for (char c: count) {
45+
chars[cur++] = c;
46+
}
47+
}
48+
return cur;
49+
}
50+
};
51+
52+
#endif //LEETCODE_443_HPP

algorithms/p443/443_test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
#include "443.hpp"
3+
#include <gtest/gtest.h>
4+
5+
TEST(p443, example) {
6+
Solution s;
7+
vector<char> arr = {'a', 'a', 'b', 'b', 'c', 'c', 'c'};
8+
ASSERT_EQ(6, s.compress(arr));
9+
}

algorithms/p652/652.hpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
#ifndef LEETCODE_652_HPP
3+
#define LEETCODE_652_HPP
4+
5+
#include <iostream>
6+
#include <queue>
7+
#include <algorithm>
8+
#include <vector>
9+
#include <unordered_map>
10+
#include <unordered_set>
11+
#include <set>
12+
#include <numeric>
13+
#include <stack>
14+
#include <string>
15+
#include "../common/leetcode.hpp"
16+
17+
using namespace std;
18+
19+
class Solution {
20+
public:
21+
vector<TreeNode *> findDuplicateSubtrees(TreeNode *root) {
22+
trees.clear();
23+
res.clear();
24+
postorder(root);
25+
return res;
26+
}
27+
28+
private:
29+
unordered_map<string, int> trees;
30+
vector<TreeNode *> res;
31+
32+
string postorder(TreeNode *node) {
33+
if (node == nullptr) {
34+
return "#";
35+
}
36+
string serial = to_string(node->val) + "," + (postorder(node->left)) + "," + (postorder(node->right));
37+
trees[serial]++;
38+
if (trees[serial] == 2)
39+
res.emplace_back(node);
40+
return serial;
41+
}
42+
};
43+
44+
#endif //LEETCODE_652_HPP

algorithms/p652/652_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
#include "652.hpp"
3+
#include <gtest/gtest.h>
4+
5+
TEST(p652, example) {
6+
Solution s;
7+
}

0 commit comments

Comments
 (0)