Skip to content

Commit cd618ff

Browse files
committed
ADD: update leetcode solutions
1 parent fdc4546 commit cd618ff

13 files changed

+533
-0
lines changed

leetcode/CanIWin.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
vector<bool> isWin(1<<20);
5+
vector<bool> isLose(1<<20);
6+
class Solution {
7+
public:
8+
bool canIWin(int maxChoosableInteger, int desiredTotal) {
9+
if (maxChoosableInteger * (maxChoosableInteger + 1) / 2 < desiredTotal) {
10+
return false;
11+
}
12+
if (desiredTotal <= 0) {
13+
return true;
14+
}
15+
fill(isWin.begin(), isWin.begin() + (1 << maxChoosableInteger), false);
16+
fill(isLose.begin(), isLose.begin() + (1 << maxChoosableInteger), false);
17+
this->mx = maxChoosableInteger;
18+
return solve(0, desiredTotal);
19+
}
20+
private:
21+
int mx;
22+
bool solve(int status, int total) {
23+
if (isWin[status]) return true;
24+
if (isLose[status]) return false;
25+
if (total <= 0) {
26+
isLose[status] = true;
27+
return false;
28+
}
29+
for (int i = 1; i <= this->mx; ++i) {
30+
if (!((1 << (i - 1)) & status)) {
31+
bool nt = solve(status | (1 << (i - 1)), total - i);
32+
if (!nt) {
33+
isWin[status] = true;
34+
return true;
35+
}
36+
}
37+
}
38+
isLose[status] = true;
39+
return false;
40+
}
41+
};
42+
43+
int main() {
44+
Solution sol;
45+
cout << sol.canIWin(10, 11) << endl;
46+
for (int i = 10; i <= 160; ++i) {
47+
cout << i << " " << sol.canIWin(20, i) << endl;
48+
}
49+
return 0;
50+
}

leetcode/DeleteNodeInBST.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
struct TreeNode {
5+
int val;
6+
TreeNode *left;
7+
TreeNode *right;
8+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
9+
};
10+
11+
class Solution {
12+
private:
13+
bool find_key(TreeNode* root, int key, TreeNode*& parent, TreeNode*& target) {
14+
parent = NULL;
15+
target = root;
16+
while (target != NULL && target->val != key) {
17+
if (target->val < key) {
18+
parent = target;
19+
target = target->right;
20+
} else {
21+
parent = target;
22+
target = target->left;
23+
}
24+
}
25+
return (target != NULL);
26+
}
27+
public:
28+
TreeNode* deleteNode(TreeNode* root, int key) {
29+
if (NULL == root) {
30+
return root;
31+
}
32+
TreeNode *parent = NULL, *to_del = NULL;
33+
if (!find_key(root, key, parent, to_del)) {
34+
return root;
35+
}
36+
assert(to_del->val == key);
37+
38+
if (to_del->left == NULL && to_del->right == NULL) {
39+
if (parent == NULL) {
40+
return NULL;
41+
} else if (parent->left == to_del) {
42+
parent->left = NULL;
43+
} else {
44+
parent->right = NULL;
45+
}
46+
return root;
47+
} else if (to_del->left == NULL) {
48+
if (parent == NULL) {
49+
return to_del->right;
50+
} else if (parent->left == to_del) {
51+
parent->left = to_del->right;
52+
} else {
53+
parent->right = to_del->right;
54+
}
55+
return root;
56+
} else if (to_del->right == NULL) {
57+
if (parent == NULL) {
58+
return to_del->left;
59+
} else if (parent->left == to_del) {
60+
parent->left = to_del->left;
61+
} else {
62+
parent->right = to_del->left;
63+
}
64+
return root;
65+
} else {
66+
TreeNode* r_node = to_del->right;
67+
if (r_node->left == NULL) {
68+
r_node->left = to_del->left;
69+
if (parent == NULL) {
70+
return r_node;
71+
} else if (parent->left == to_del) {
72+
parent->left = r_node;
73+
} else {
74+
parent->right = r_node;
75+
}
76+
return root;
77+
} else {
78+
TreeNode* pl_node = r_node;
79+
TreeNode* l_node = r_node->left;
80+
while (l_node->left != NULL) {
81+
pl_node = pl_node->left;
82+
l_node = l_node->left;
83+
}
84+
pl_node->left = l_node->right;
85+
l_node->left = to_del->left;
86+
l_node->right = to_del->right;
87+
if (parent == NULL) {
88+
return l_node;
89+
} else if (parent->left == to_del) {
90+
parent->left = l_node;
91+
} else {
92+
parent->right = l_node;
93+
}
94+
return root;
95+
}
96+
}
97+
}
98+
};
99+
100+
int main() {
101+
return 0;
102+
}

leetcode/EncodeAndDecodeTinyURL.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from random import randint
2+
3+
class Codec:
4+
5+
ch_list = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
6+
7+
def __init__(self):
8+
self.forward_mp = {}
9+
self.backward_mp = {}
10+
11+
def encode(self, longUrl):
12+
"""Encodes a URL to a shortened URL.
13+
14+
:type longUrl: str
15+
:rtype: str
16+
"""
17+
while True:
18+
s = 'http://tinyurl.com/' + self._get_random_str()
19+
if not s in self.forward_mp:
20+
self.forward_mp[longUrl] = s
21+
self.backward_mp[s] = longUrl
22+
return s
23+
24+
def decode(self, shortUrl):
25+
"""Decodes a shortened URL to its original URL.
26+
27+
:type shortUrl: str
28+
:rtype: str
29+
"""
30+
return self.backward_mp[shortUrl]
31+
32+
def _get_random_str(self):
33+
return ''.join([Codec.ch_list[randint(0, len(Codec.ch_list) - 1)] for i in range(10)])
34+
35+
# Your Codec object will be instantiated and called as such:
36+
codec = Codec()
37+
s = codec.encode('http://baidu.com')
38+
print(s)
39+
print(codec.decode(s))

leetcode/FindComplement.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
int findComplement(int num) {
7+
for (int i = 31; i >= 0; --i) {
8+
if ((1 << i) & num) {
9+
int res = 0;
10+
for (int j = i - 1; j >= 0; --j) {
11+
if (!((1 << j) & num)) {
12+
res += (1 << j);
13+
}
14+
}
15+
return res;
16+
}
17+
}
18+
return 0;
19+
}
20+
};
21+
22+
int main() {
23+
return 0;
24+
}

leetcode/FindMaxForm.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int dp[2][105][105];
5+
class Solution {
6+
public:
7+
int findMaxForm(vector<string>& strs, int m, int n) {
8+
for (int i = 0; i < 2; ++i) {
9+
for (int j = 0; j <= m; ++j) {
10+
for (int k = 0; k <= n; ++k) {
11+
dp[i][j][k] = 0;
12+
}
13+
}
14+
}
15+
int sz = strs.size();
16+
vector<int> zeros(sz, 0), ones(sz, 0);
17+
for (int i = 0; i < strs.size(); ++i) {
18+
string& s = strs[i];
19+
for (int j = 0; j < s.size(); ++j) {
20+
if (s[j] == '0') ++zeros[i];
21+
else ++ones[i];
22+
}
23+
}
24+
for (int i = 0; i < sz; ++i) {
25+
for (int j = 0; j <= m; ++j) {
26+
for (int k = 0; k <= n; ++k) {
27+
int cur = i % 2;
28+
int prev = 1 - cur;
29+
int r1 = dp[prev][j][k];
30+
int r2 = 0;
31+
if (j >= zeros[i] && k >= ones[i]) {
32+
r2 = dp[prev][j - zeros[i]][k - ones[i]] + 1;
33+
}
34+
dp[cur][j][k] = max(r1, r2);
35+
}
36+
}
37+
}
38+
return dp[(sz - 1) % 2][m][n];
39+
}
40+
};
41+
42+
int main() {
43+
return 0;
44+
}

leetcode/HammingDistance.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
int hammingDistance(int x, int y) {
4+
return __builtin_popcount(x ^ y);
5+
}
6+
};

leetcode/Heaters.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
int findRadius(vector<int>& houses, vector<int>& heaters) {
7+
if (houses.empty()) {
8+
return 0;
9+
}
10+
sort(heaters.begin(), heaters.end());
11+
int mx = 0;
12+
for (int i = 0; i < houses.size(); ++i) {
13+
vector<int>::iterator it = lower_bound(heaters.begin(), heaters.end(), houses[i]);
14+
if (it == heaters.end()) {
15+
mx = max(mx, houses[i] - heaters.back());
16+
} else if (it == heaters.begin()) {
17+
mx = max(mx, heaters[0] - houses[i]);
18+
} else {
19+
mx = max(mx, min(*it - houses[i], houses[i] - *(it - 1)));
20+
}
21+
}
22+
return mx;
23+
}
24+
};
25+
26+
int main() {
27+
return 0;
28+
}

leetcode/MatchsticksToSquare.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution {
5+
private:
6+
vector<int> dp;
7+
vector<int> len;
8+
int edge_len, sz;
9+
bool solve(int status) {
10+
if (dp[status] > 0) {
11+
return true;
12+
} else if (dp[status] < 0) {
13+
return false;
14+
}
15+
int sum = 0;
16+
for (int i = 0; i < sz; ++i) {
17+
if ((1 << i) & status) {
18+
continue;
19+
}
20+
sum += len[i];
21+
}
22+
int rem = sum % edge_len;
23+
if (rem == 0) {
24+
rem = edge_len;
25+
}
26+
for (int i = 0; i < sz; ++i) {
27+
if ((1 << i) & status) {
28+
continue;
29+
}
30+
if (len[i] > rem) {
31+
continue;
32+
}
33+
if (solve(status | (1 << i))) {
34+
dp[status] = 1;
35+
return true;
36+
}
37+
}
38+
dp[status] = -1;
39+
return false;
40+
}
41+
public:
42+
bool makesquare(vector<int>& nums) {
43+
int total = accumulate(nums.begin(), nums.end(), 0);
44+
if (total % 4 || nums.size() < 4) {
45+
return false;
46+
}
47+
len = nums;
48+
edge_len = total / 4;
49+
sz = nums.size();
50+
dp.resize(1 << sz);
51+
fill(dp.begin(), dp.end(), 0);
52+
dp[(1 << sz) - 1] = 1;
53+
return solve(0);
54+
}
55+
};
56+
57+
int main() {
58+
Solution sol;
59+
// std::vector<int> v = {5,5,5,5,16,4,4,4,4,4,3,3,3,3,4};
60+
std::vector<int> v = {1,1,2,2,2};
61+
cout << sol.makesquare(v) << endl;
62+
std::vector<int> u = {3, 3, 3, 3, 4};
63+
cout << sol.makesquare(u) << endl;
64+
return 0;
65+
}

0 commit comments

Comments
 (0)