Skip to content

Commit bb93323

Browse files
author
Liang Wang
committed
update
1 parent ed2b685 commit bb93323

7 files changed

+179
-0
lines changed

leetcode/CanPlaceFlowers.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
4+
int ptr = 0;
5+
int res = 0;
6+
while (ptr < flowerbed.size()) {
7+
if (flowerbed[ptr] == 1) {
8+
++ptr;
9+
continue;
10+
}
11+
int start = ptr;
12+
while (ptr < flowerbed.size() && flowerbed[ptr] == 0) {
13+
++ptr;
14+
}
15+
int slots = ptr - start;
16+
if (start > 0) --slots;
17+
if (ptr < flowerbed.size()) --slots;
18+
res += (max(slots, 0) + 1) / 2;
19+
}
20+
return res >= n;
21+
}
22+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
class Solution {
11+
public:
12+
string tree2str(TreeNode* t) {
13+
string res;
14+
if (NULL == t) {
15+
return res;
16+
}
17+
res += to_string(t->val);
18+
if (t->left == NULL && t->right == NULL) {
19+
return res;
20+
} else if (t->left != NULL && t->right == NULL) {
21+
res += "(";
22+
res += tree2str(t->left);
23+
res.push_back(')');
24+
return res;
25+
} else if (t->left == NULL && t->right != NULL) {
26+
res += "()(";
27+
res += tree2str(t->right);
28+
res += ")";
29+
return res;
30+
} else {
31+
res.push_back('(');
32+
res += tree2str(t->left);
33+
res += ")(";
34+
res += tree2str(t->right);
35+
res.push_back(')');
36+
return res;
37+
}
38+
}
39+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public:
3+
vector<vector<string>> findDuplicate(vector<string>& paths) {
4+
vector<vector<string>> res;
5+
map<string, vector<string> > content2path;
6+
for (auto& path : paths) {
7+
bool in_dir = true, in_name = false, in_content = false;
8+
string dir, name, content;
9+
for (int i = 0; i < path.size(); ++i) {
10+
if (in_dir && path[i] != ' ') {
11+
dir.push_back(path[i]);
12+
} else if (in_dir && path[i] == ' ') {
13+
in_dir = false;
14+
dir.push_back('/');
15+
in_name = true;
16+
} else if (in_name && path[i] != '(' && path[i] != ' ') {
17+
name.push_back(path[i]);
18+
} else if (in_name && path[i] == '(') {
19+
in_name = false;
20+
in_content = true;
21+
} else if (in_content && path[i] != ')') {
22+
content.push_back(path[i]);
23+
} else if (in_content && path[i] == ')') {
24+
in_content = false;
25+
in_name = true;
26+
if (content2path.find(content) == content2path.end()) {
27+
vector<string> v;
28+
content2path[content] = v;
29+
}
30+
content2path[content].push_back(dir + name);
31+
content.clear();
32+
name.clear();
33+
}
34+
}
35+
}
36+
for (auto it = content2path.begin(); it != content2path.end(); ++it) {
37+
if (it->second.size() > 1) {
38+
res.push_back(it->second);
39+
}
40+
}
41+
return res;
42+
}
43+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int findLHS(vector<int>& nums) {
4+
sort(nums.begin(), nums.end());
5+
int res = 0;
6+
int ptr = 0;
7+
for (int i = 0; i < nums.size(); ++i) {
8+
if (i - 1 >= 0 && nums[i - 1] == nums[i]) {
9+
continue;
10+
}
11+
while (ptr < nums.size() && nums[ptr] - nums[i] <= 1) {
12+
++ptr;
13+
}
14+
if (nums[ptr - 1] == nums[i] + 1) {
15+
res = max(res, ptr - i);
16+
}
17+
}
18+
return res;
19+
}
20+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
4+
map<string, int> name2idx;
5+
for (int i = 0; i < list1.size(); ++i) {
6+
name2idx[list1[i]] = i;
7+
}
8+
int mn = list1.size() + list2.size();
9+
vector<string> res;
10+
for (int i = 0; i < list2.size(); ++i) {
11+
if (name2idx.find(list2[i]) != name2idx.end()) {
12+
int sm = i + name2idx[list2[i]];
13+
if (sm < mn) {
14+
res.clear();
15+
res.push_back(list2[i]);
16+
mn = sm;
17+
} else if (sm == mn) {
18+
res.push_back(list2[i]);
19+
}
20+
}
21+
}
22+
return res;
23+
}
24+
};

leetcode/RangeAdditionII.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int maxCount(int m, int n, vector<vector<int>>& ops) {
4+
if (ops.empty()) {
5+
return m * n;
6+
}
7+
int r = m, c = n;
8+
for (int i = 0; i < ops.size(); ++i) {
9+
int nr = ops[i][0], nc = ops[i][1];
10+
r = min(r, nr);
11+
c = min(c, nc);
12+
}
13+
return r * c;
14+
}
15+
};

leetcode/ValidTriangleNumber.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int triangleNumber(vector<int>& nums) {
4+
int res = 0;
5+
sort(nums.begin(), nums.end());
6+
for (int i = 0; i < nums.size(); ++i) {
7+
for (int j = 0; j < i; ++j) {
8+
if (nums[j] > 0) {
9+
int idx = lower_bound(nums.begin(), nums.end(), nums[i] + nums[j]) - nums.begin();
10+
res += idx - (i + 1);
11+
}
12+
}
13+
}
14+
return res;
15+
}
16+
};

0 commit comments

Comments
 (0)