Skip to content

[pull] master from begeekmyfriend:master #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions 0043_multiply_strings/multiply_strings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,29 @@ using namespace std;
class Solution {
public:
string multiply(string num1, string num2) {
string res(num1.length() + num2.length(), '0');
vector<int> v(num1.length() + num2.length());
for (int i = num2.length() - 1; i >= 0; i--) {
int j, carry = 0;
for (j = num1.length() - 1; j >= 0; j--) {
carry += (num1[j] - '0') * (num2[i] - '0') + (res[i + j + 1] - '0');
res[i + j + 1] = carry % 10 + '0';
carry /= 10;
for (int j = num1.length() - 1; j >= 0; j--) {
int a = num2[j] - '0';
int b = num1[i] - '0';
v[i + j + 1] = a * b;
}
res[i + j + 1] = carry + '0';
}

int i;
int i, carry = 0;
string res(v.size(), '0');
for (i = v.size() - 1; i >= 0; i--) {
carry += v[i];
res[i] += carry % 10;
carry /= 10;
}

for (i = 0; i < res.length() - 1; i++) {
if (res[i] != '0') {
break;
}
}

return res.substr(i);
}
};
42 changes: 42 additions & 0 deletions 0131_palindrome_patitioning/palindrome_partition.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <bits/stdc++.h>

using namespace std;

class Solution {
public:
vector<vector<string>> partition(string s) {
vector<vector<string>> res;
vector<vector<bool>> isPalindrome(s.size(), vector<bool>(s.size(), true));

// From bottom to up
for (int i = s.length() - 1; i >= 0; i--) {
// From left to right
for (int j = i + 1; j < s.length(); j++) {
// notebook for palindrome substring judgement
isPalindrome[i][j] = s[i] == s[j] && isPalindrome[i + 1][j - 1];
}
}

dfs(s, 0, isPalindrome, res);
return res;
}

private:
vector<string> ans;
void dfs(const string& s, int start, vector<vector<bool>>& isPalindrome, vector<vector<string>>& res) {
// DFS for combination. When the start index reaches to the end, all
// the substrings are collected.
if (start == s.length()) {
res.push_back(ans);
return;
}

for (int i = start; i < s.length(); i++) {
if (isPalindrome[start][i]) {
ans.push_back(s.substr(start, i - start + 1));
dfs(s, i + 1, isPalindrome, res);
ans.pop_back();
}
}
}
};
65 changes: 65 additions & 0 deletions 0133_clone_graph/clone_graph.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <bits/stdc++.h>

using namespace std;

// Definition for a Node.
class Node {
public:
int val;
vector<Node*> neighbors;
Node() {
val = 0;
neighbors = vector<Node*>();
}
Node(int _val) {
val = _val;
neighbors = vector<Node*>();
}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};

class Solution {
public:
Node* cloneGraph(Node* node) {
if (node == nullptr) {
return node;
}
#if 1 // DFS
if (cloned.find(node) != cloned.end()) {
return cloned[node];
}

cloned[node] = new Node(node->val);

for (auto& neighbor : node->neighbors) {
cloned[node]->neighbors.emplace_back(cloneGraph(neighbor));
}
#else // BFS
queue<Node *> q;
q.push(node);
cloned[node] = new Node(node->val);

while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
auto n = q.front();
q.pop();

for (auto& neighbor : n->neighbors) {
if (cloned.find(neighbor) == cloned.end()) {
cloned[neighbor] = new Node(neighbor->val);
q.push(neighbor);
}
cloned[n]->neighbors.emplace_back(cloned[neighbor]);
}
}
}
#endif
return cloned[node];
}
private:
unordered_map<Node *, Node *> cloned;
};
10 changes: 6 additions & 4 deletions 0567_permutation_in_string/permutation_in_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ class Solution {
count[c]++;
}

int l = 0, r = 0, len = 0;
int l = 0, r = 0, hits = 0;
while (r < s2.length()) {
if (--count[s2[r++]] >= 0) {
len++;
hits++;
}

// When the window length equals to the hit length,
// the permutation is contained.
if (r - l >= s1.length()) {
if (len == s1.length()) {
if (hits == s1.length()) {
return true;
}
if (++count[s2[l++]] > 0) {
len--;
hits--;
}
}
}
Expand Down