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
19 changes: 19 additions & 0 deletions non-overlapping-intervals/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
static bool comp(const vector<int> &a, const vector<int> &b) {
return (a[1] == b[1] ? a[0] > b[0] : a[1] < b[1]);
}
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(), comp);
int ans = 0, prev_end = intervals[0][1], start, end;
for(int i = 1; i < intervals.size(); i++) {
if(prev_end > intervals[i][0])
ans++;
else
prev_end = intervals[i][1];
}

return ans;
}
};

53 changes: 53 additions & 0 deletions remove-nth-node-from-end-of-list/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* first = head;
for(int i = 0; i < n; i++)
first = first->next;

ListNode* dummy = new ListNode(-1, head);
ListNode* second = dummy;
while(first != nullptr) {
first = first->next;
second = second->next;
}

second->next = second->next->next;
return dummy->next;
}

// ListNode* removeNthFromEnd(ListNode* head, int n) {
// stack<ListNode*> sta;
// ListNode* curr = head;

// while(curr) {
// sta.push(curr);
// curr = curr->next;
// }

// ListNode* prev = nullptr;
// while(n > 1) {
// n--;
// prev = sta.top();
// sta.pop();
// }
// sta.pop();
// if(sta.empty())
// return prev;
// else {
// sta.top()->next = prev;
// return head;
// }
// }
};

31 changes: 31 additions & 0 deletions same-tree/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool ans = true;
void rec(TreeNode* p, TreeNode* q) {
if(p == nullptr && q == nullptr)
return;
else if(p && q && p->val == q->val) {
rec(p->left, q->left);
rec(p->right, q->right);
}
else
ans = false;
}

bool isSameTree(TreeNode* p, TreeNode* q) {
rec(p, q);
return ans;
}
};

92 changes: 92 additions & 0 deletions serialize-and-deserialize-binary-tree/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Codec {
public:

// Encodes a tree to a single string.
string serialize(TreeNode* root) {
queue<TreeNode*> que;
string str = "root = [";

if(root != nullptr) {
que.push(root);
str += to_string(root->val);
}

while(!que.empty()) {
TreeNode* curr = que.front();
que.pop();
if(curr->left != nullptr) {
que.push(curr->left);
str += "," + to_string(curr->left->val);
}
else
str += ",null";

if(curr->right != nullptr) {
que.push(curr->right);
str += "," + to_string(curr->right->val);
}
else
str += ",null";
}
str += "]";
return str;
}

// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
int start = data.find('[') + 1;
int end = data.find_last_of(']');

if (start >= end) return nullptr;

string content = data.substr(start, end - start);

vector<string> nodes;
stringstream ss(content);
string temp;
while (getline(ss, temp, ','))
nodes.push_back(temp);

int idx = 0;
TreeNode* root = new TreeNode(stoi(nodes[idx++]));
queue<TreeNode*> que;
que.push(root);

while(!que.empty() && idx < nodes.size()) {
TreeNode* curr = que.front();
que.pop();

if(nodes[idx] == "null") {
curr->left = nullptr;
idx++;
} else {
curr->left = new TreeNode(stoi(nodes[idx++]));
que.push(curr->left);
}

if(nodes[idx] == "null") {
curr->right = nullptr;
idx++;
} else {
curr->right = new TreeNode(stoi(nodes[idx++]));
que.push(curr->right);
}
}

return root;
}
};

// Your Codec object will be instantiated and called as such:
// Codec ser, deser;
// TreeNode* ans = deser.deserialize(ser.serialize(root));