Skip to content

Commit 71e6c1d

Browse files
committed
update leetcode
1 parent cd618ff commit 71e6c1d

File tree

7 files changed

+213
-0
lines changed

7 files changed

+213
-0
lines changed

leetcode/LicenseKeyFormatting.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
string licenseKeyFormatting(string S, int K) {
7+
reverse(S.begin(), S.end());
8+
string res;
9+
int cur = 0;
10+
for (int i = 0; i < S.size(); ++i) {
11+
if (S[i] == '-') {
12+
continue;
13+
} else if (S[i] <= 'z' && S[i] >= 'a') {
14+
res.push_back(S[i] - 'a' + 'A');
15+
} else {
16+
res.push_back(S[i]);
17+
18+
}
19+
++cur;
20+
if (cur == K) {
21+
res.push_back('-');
22+
cur = 0;
23+
}
24+
}
25+
if (res.back() == '-') {
26+
res.pop_back();
27+
}
28+
reverse(res.begin(), res.end());
29+
return res;
30+
}
31+
};

leetcode/MagicalString.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 magicalString(int n) {
7+
string digits = "122";
8+
int ptr = 2, filler = 1;
9+
while (digits.size() < n) {
10+
for (int i = 0; i < digits[ptr] - '0'; ++i) {
11+
digits.push_back('0' + filler);
12+
}
13+
++ptr;
14+
filler = (filler == 1) ? 2 : 1;
15+
}
16+
int res = 0;
17+
for (int i = 0; i < n; ++i) {
18+
res += digits[i] == '1';
19+
}
20+
return res;
21+
}
22+
};
23+
24+
int main() {
25+
Solution sol;
26+
cout << sol.magicalString(7) << endl;
27+
return 0;
28+
}

leetcode/MaxConsecutiveOnes.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int findMaxConsecutiveOnes(vector<int>& nums) {
4+
int res = 0;
5+
int ptr = 0;
6+
while (ptr < nums.size()) {
7+
if (nums[ptr] == 1) {
8+
int end = ptr + 1;
9+
while (end < nums.size() && nums[end] == 1) ++end;
10+
res = max(res, end - ptr);
11+
ptr = end + 1;
12+
} else {
13+
++ptr;
14+
}
15+
}
16+
return res;
17+
}
18+
};

leetcode/MedianSlidingWindow.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
vector<double> medianSlidingWindow(vector<int>& nums, int k) {
7+
vector<double> res;
8+
if (nums.empty()) {
9+
return res;
10+
}
11+
multiset<int> left, right;
12+
for (int i = 0; i < (int)nums.size(); ++i) {
13+
if (left.empty() || nums[i] <= *left.rbegin()) {
14+
left.insert(nums[i]);
15+
} else {
16+
right.insert(nums[i]);
17+
}
18+
if (i >= k) {
19+
auto it = left.find(nums[i - k]);
20+
if (it != left.end()) {
21+
left.erase(it);
22+
} else {
23+
right.erase(right.find(nums[i - k]));
24+
}
25+
}
26+
int expected_size = (left.size() + right.size()) / 2;
27+
while ((int)right.size() < expected_size) {
28+
right.insert(*left.rbegin());
29+
auto it = left.end();
30+
--it;
31+
left.erase(it);
32+
}
33+
while ((int)right.size() > expected_size) {
34+
left.insert(*right.begin());
35+
right.erase(right.begin());
36+
}
37+
if (i < k - 1) {
38+
continue;
39+
}
40+
if (k % 2 == 1) {
41+
res.push_back(*left.rbegin() * 1.0);
42+
} else {
43+
res.push_back(*left.rbegin() * 0.5 + *right.begin() * 0.5);
44+
}
45+
}
46+
return res;
47+
}
48+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
int minMutation(string start, string end, vector<string>& bank) {
4+
const char genes[4] = {'A', 'C', 'G', 'T'};
5+
set<string> visited, bank_set;
6+
for (int i = 0; i < bank.size(); ++i) {
7+
bank_set.insert(bank[i]);
8+
}
9+
queue<pair<string, int> > q;
10+
q.push({start, 0});
11+
visited.insert(start);
12+
while (!q.empty()) {
13+
pair<string, int> tp = q.front();
14+
q.pop();
15+
string cur = tp.first;
16+
int step = tp.second;
17+
for (int i = 0; i < 8; ++i) {
18+
char c = cur[i];
19+
for (int j = 0; j < 4; ++j) {
20+
if (genes[j] != c) {
21+
cur[i] = genes[j];
22+
if (visited.find(cur) != visited.end() || bank_set.find(cur) == bank_set.end()) {
23+
continue;
24+
}
25+
if (cur == end) {
26+
return step + 1;
27+
}
28+
q.push({cur, step + 1});
29+
visited.insert(cur);
30+
}
31+
}
32+
cur[i] = c;
33+
}
34+
}
35+
return -1;
36+
}
37+
};

leetcode/PredictTheWinner.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
bool PredictTheWinner(vector<int>& nums) {
7+
int sz = nums.size();
8+
vector<vector<int> > player1(sz, vector<int>(sz + 1));
9+
vector<vector<int>> player2(sz, vector<int>(sz + 1));
10+
for (int len = 1; len <= sz; ++len) {
11+
for (int i = 0; i + len <= sz; ++i) {
12+
int sum = accumulate(nums.begin() + i, nums.begin() + i + len, 0);
13+
// for player1
14+
if (len == 1) {
15+
player1[i][len] = nums[i];
16+
} else {
17+
player1[i][len] = max(sum - player2[i + 1][len - 1], sum - player2[i][len - 1]);
18+
}
19+
// for player 2
20+
if (len == 1) {
21+
player2[i][len] = nums[i];
22+
} else {
23+
player2[i][len] = max(sum - player1[i + 1][len - 1], sum - player1[i][len - 1]);
24+
}
25+
}
26+
}
27+
return player1[0][sz] * 2 >= accumulate(nums.begin(), nums.end(), 0);
28+
}
29+
};

leetcode/TotalHammingDistance.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
int totalHammingDistance(vector<int>& nums) {
7+
int res = 0;
8+
for (int i = 0; i < 32; ++i) {
9+
int zeros = 0, ones = 0, mask = (1 << i);
10+
for (auto n : nums) {
11+
if (mask & n) ++ones;
12+
else ++zeros;
13+
}
14+
res += ones * zeros;
15+
}
16+
return res;
17+
}
18+
};
19+
20+
int main() {
21+
return 0;
22+
}

0 commit comments

Comments
 (0)