Skip to content
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
52 changes: 52 additions & 0 deletions clone-graph/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
// 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)
return nullptr;


Node* clone = new Node(node->val);
map<Node*, Node*> clones;
queue<Node*> que;

clones[node] = clone;
que.push(node);

while(!que.empty()) {
node = que.front();
que.pop();

for(auto neigh : node->neighbors) {
if(clones.find(neigh) == clones.end()) {
que.push(neigh);
clones[neigh] = new Node(neigh->val);
}
clones[node]->neighbors.push_back(clones[neigh]);
}
}

return clone;
}
};

18 changes: 18 additions & 0 deletions longest-common-subsequence/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
int n = text1.size(), m = text2.size();
vector<vector<int>> dp(n, vector<int> (m, 0));

for(int i = 0; i < n ; i++) {
for(int j = 0; j < m; j++) {
dp[i][j] = max((i == 0 ? 0 : dp[i - 1][j]), (j == 0 ? 0 : dp[i][j - 1]));
if(text1[i] == text2[j])
dp[i][j] = (i == 0 || j == 0 ? 0 : dp[i - 1][j - 1]) + 1;
}
}

return dp[n - 1][m - 1];
}
};

22 changes: 22 additions & 0 deletions longest-repeating-character-replacement/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
int characterReplacement(string s, int k) {
int left = 0, right = 0, ans = 0;
int maxi_cnt = 0;
map<char, int> cnt;

while(left < s.size() && right < s.size()) {
cnt[s[right]]++;
maxi_cnt = max(maxi_cnt, cnt[s[right]]);
right++;

if(right - left - maxi_cnt <= k)
ans = max(ans, right - left);
else
cnt[s[left++]]--;
}

return ans;
}
};

24 changes: 24 additions & 0 deletions palindromic-substrings/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
int countSubstrings(string s) {
int ans = 0, n = s.size();
for(int i = 0; i < n; i++) {
int left = i, right = i;
while(left >= 0 && right < n && s[left] == s[right]) {
ans++;
left--;
right++;
}

left = i, right = i + 1;
while(left >= 0 && right < n && s[left] == s[right]) {
ans++;
left--;
right++;
}
}

return ans;
}
};

36 changes: 36 additions & 0 deletions reverse-bits/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// class Solution {
// public:
// int reverseBits(int n) {
// string bits = "";

// while(n) {
// bits += (n % 2) + '0';
// n /= 2;
// }

// while(bits.size() < 32)
// bits += '0';

// int ans = 0, d = 1;
// for(int i = 31; i >= 0; i--) {
// ans += d * (bits[i] - '0');
// d <<= 1;
// }

// return ans;
// }
// };

class Solution {
public:
int reverseBits(int n) {
int ans = 0;
for(int i = 0; i < 32; i++) {
ans <<= 1;
ans |= (n & 1);
n >>= 1;
}
return ans;
}
};