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

Commit a25366e

Browse files
committed
8 problems
1 parent 7d3d406 commit a25366e

8 files changed

+370
-0
lines changed

array-nesting_1_AC.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Permutation group.
2+
#include <algorithm>
3+
#include <vector>
4+
using std::max;
5+
using std::vector;
6+
7+
class Solution {
8+
public:
9+
int arrayNesting(vector<int>& nums) {
10+
int n = nums.size();
11+
vector<bool> vb(n, false);
12+
13+
int i, j;
14+
int res;
15+
int cnt;
16+
17+
res = 0;
18+
j = 0;
19+
while (j < n) {
20+
if (vb[j]) {
21+
++j;
22+
continue;
23+
}
24+
25+
i = j;
26+
cnt = 0;
27+
while (!vb[i]) {
28+
vb[i] = true;
29+
i = nums[i];
30+
++cnt;
31+
}
32+
res = max(res, cnt);
33+
++j;
34+
}
35+
vb.clear();
36+
37+
return res;
38+
}
39+
};

can-place-flowers_1_AC.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// If you don't one-shot, you're dead.
2+
#include <vector>
3+
using std::vector;
4+
5+
class Solution {
6+
public:
7+
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
8+
auto &a = flowerbed;
9+
int na = a.size();
10+
int i, j;
11+
int cnt;
12+
int len;
13+
14+
cnt = 0;
15+
i = 0;
16+
while (i < na) {
17+
if (a[i] == 1) {
18+
++i;
19+
continue;
20+
}
21+
j = i + 1;
22+
while (j < na && a[j] == 0) {
23+
++j;
24+
}
25+
len = j - i;
26+
if (i == 0) {
27+
++len;
28+
}
29+
if (j == na) {
30+
++len;
31+
}
32+
cnt += (len - 1) / 2;
33+
i = j;
34+
}
35+
return cnt >= n;
36+
}
37+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
#include <string>
11+
using std::string;
12+
using std::to_string;
13+
14+
class Solution {
15+
public:
16+
string tree2str(TreeNode* t) {
17+
if (t == NULL) {
18+
return "";
19+
}
20+
21+
string res = to_string(t->val);
22+
if (t->right != NULL) {
23+
res += "(" + tree2str(t->left) + ")(" + tree2str(t->right) + ")";
24+
} else if (t->left != NULL) {
25+
res += "(" + tree2str(t->left) + ")";
26+
}
27+
return res;
28+
}
29+
};
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include <string>
2+
#include <unordered_map>
3+
#include <vector>
4+
using std::string;
5+
using std::unordered_map;
6+
using std::vector;
7+
8+
class Solution {
9+
public:
10+
vector<vector<string>> findDuplicate(vector<string>& paths) {
11+
vector<string> vt;
12+
int nt;
13+
int i;
14+
string path, fname, content;
15+
unordered_map<string, vector<string>> um;
16+
17+
for (string &s: paths) {
18+
tokenize(s, vt);
19+
nt = vt.size();
20+
path = vt[0];
21+
for (i = 1; i < nt; ++i) {
22+
parse(vt[i], fname, content);
23+
um[content].push_back(path + "/" + fname);
24+
}
25+
}
26+
27+
vector<vector<string>> res;
28+
for (auto it = um.begin(); it != um.end(); ++it) {
29+
if (it->second.size() > 1) {
30+
res.push_back(it->second);
31+
}
32+
}
33+
um.clear();
34+
35+
return res;
36+
}
37+
private:
38+
void tokenize(const string &s, vector<string> &vt) {
39+
int ls = s.size();
40+
int cnt_br = 0;
41+
int i = 0;
42+
43+
string ss = "";
44+
vt.clear();
45+
while (true) {
46+
while (i < ls && s[i] == ' ' && cnt_br == 0) {
47+
++i;
48+
}
49+
if (i >= ls) {
50+
break;
51+
}
52+
53+
while (i < ls) {
54+
if (cnt_br == 0 && s[i] == ' ') {
55+
break;
56+
}
57+
if (s[i] == '(') {
58+
++cnt_br;
59+
} else if (s[i] == ')') {
60+
--cnt_br;
61+
}
62+
ss.push_back(s[i]);
63+
++i;
64+
}
65+
vt.push_back(ss);
66+
ss.clear();
67+
if (i >= ls) {
68+
break;
69+
}
70+
}
71+
}
72+
73+
void parse(const string &s, string &fname, string &content) {
74+
int ls = s.size();
75+
int i;
76+
77+
fname = "";
78+
content = "";
79+
i = 0;
80+
while (s[i] != '(') {
81+
fname.push_back(s[i++]);
82+
}
83+
++i;
84+
while (s[i] != ')') {
85+
content.push_back(s[i++]);
86+
}
87+
}
88+
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Lame, dude.
2+
#include <algorithm>
3+
#include <string>
4+
#include <unordered_map>
5+
#include <vector>
6+
using std::min;
7+
using std::string;
8+
using std::unordered_map;
9+
using std::vector;
10+
11+
class Solution {
12+
public:
13+
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
14+
unordered_map<string, int> um;
15+
int n1 = list1.size();
16+
int n2 = list2.size();
17+
18+
for (string &s: list1) {
19+
++um[s];
20+
}
21+
for (string &s: list2) {
22+
++um[s];
23+
}
24+
25+
auto it = um.begin();
26+
while (it != um.end()) {
27+
if (it->second == 1) {
28+
it = um.erase(it);
29+
} else {
30+
it->second = 0;
31+
++it;
32+
}
33+
}
34+
35+
int i;
36+
for (i = 0; i < n1; ++i) {
37+
if (um.find(list1[i]) != um.end()) {
38+
um[list1[i]] += i;
39+
}
40+
}
41+
for (i = 0; i < n2; ++i) {
42+
if (um.find(list2[i]) != um.end()) {
43+
um[list2[i]] += i;
44+
}
45+
}
46+
47+
int min_sum = n1 + n2;
48+
for (auto &p: um) {
49+
min_sum = min(min_sum, p.second);
50+
}
51+
52+
vector<string> res;
53+
for (auto &p: um) {
54+
if (p.second == min_sum) {
55+
res.push_back(p.first);
56+
}
57+
}
58+
um.clear();
59+
60+
return res;
61+
}
62+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// That was an ugly one.
2+
#include <string>
3+
#include <vector>
4+
using std::string;
5+
using std::vector;
6+
7+
class Solution {
8+
public:
9+
int findIntegers(int num) {
10+
if (num <= 1) {
11+
return num + 1;
12+
}
13+
14+
vector<int> v;
15+
v.push_back(1);
16+
v.push_back(1);
17+
v.push_back(1);
18+
19+
int i;
20+
for (i = 3; i <= 31; ++i) {
21+
v.push_back(v[i - 1] + v[i - 2]);
22+
}
23+
24+
string s = "";
25+
while (num > 0) {
26+
s.push_back('0' + (num & 1));
27+
num >>= 1;
28+
}
29+
30+
int ls = s.size();
31+
int j;
32+
i = ls - 1;
33+
while (i > 0) {
34+
if (s[i] == '1' && s[i - 1] == '1') {
35+
s[i - 1] = '0';
36+
j = i - 2;
37+
while (j >= 0) {
38+
if (s[j] == '0') {
39+
s[j] = '1';
40+
}
41+
--j;
42+
}
43+
}
44+
--i;
45+
}
46+
47+
int res = 1;
48+
i = ls;
49+
while (i > 0) {
50+
if (s[i - 1] == '1') {
51+
res += v[i + 1];
52+
}
53+
--i;
54+
}
55+
56+
return res;
57+
}
58+
};

range-addition-ii_1_AC.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Brainteaser
2+
#include <algorithm>
3+
#include <vector>
4+
using std::min;
5+
using std::vector;
6+
7+
class Solution {
8+
public:
9+
int maxCount(int m, int n, vector<vector<int>>& ops) {
10+
int rm, rn;
11+
12+
rm = m;
13+
rn = n;
14+
for (auto &op: ops) {
15+
if (op[0] == 0 || op[1] == 0) {
16+
continue;
17+
}
18+
rm = min(rm, op[0]);
19+
rn = min(rn, op[1]);
20+
}
21+
return rm * rn;
22+
}
23+
};

tag-validator_1_AC.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// https://discuss.leetcode.com/topic/91381/short-python-accepted-but-not-sure-if-correct
2+
// I don't know how I'm gonna deal with it without using RE matching.
3+
// Don't you think this kind of problems are a bit too complicated?
4+
// Besides, C++ regex is slow.
5+
#include <regex>
6+
#include <string>
7+
using std::regex;
8+
using std::regex_replace;
9+
using std::string;
10+
11+
class Solution {
12+
public:
13+
bool isValid(string code) {
14+
if (code == "t") {
15+
return false;
16+
}
17+
18+
regex rt("<([A-Z]{1,9})>[^<]*</\\1>");
19+
regex rc("<!\\[CDATA\\[.*?\\]\\]>");
20+
string code1;
21+
22+
code = regex_replace(code, rc, "c");
23+
while (true) {
24+
code1 = regex_replace(code, rt, "t");
25+
if (code != code1) {
26+
code = code1;
27+
} else {
28+
break;
29+
}
30+
}
31+
32+
return code == "t";
33+
}
34+
};

0 commit comments

Comments
 (0)