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
23 changes: 23 additions & 0 deletions Week_01/id_28/LeetCode_101_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
return _isMirror(root, root);
}
private:
bool _isMirror(TreeNode* r1, TreeNode* r2) {
if (!r1 && !r2) return true;
if (!r1 || !r2) return false;
return (r1->val == r2->val)
&& _isMirror(r1->left, r2->right)
&& _isMirror(r1->right, r2->left);
}
};
15 changes: 15 additions & 0 deletions Week_01/id_28/LeetCode_1021_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
string removeOuterParentheses(string S) {
string s;
int l = 0;
for (char c : S) {
if (c == '(') {
if (l++ != 0) s.push_back(c);
} else {
if (l-- != 1) s.push_back(c);
}
}
return s;
}
};
14 changes: 14 additions & 0 deletions Week_01/id_28/LeetCode_1047_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
string removeDuplicates(string S) {
deque<char> d;
for (char c : S)
(!d.empty() && d.back() == c) ? d.pop_back() : d.push_back(c);
string str;
while (!d.empty()) {
str.push_back(d.front());
d.pop_front();
}
return str;
}
};
19 changes: 19 additions & 0 deletions Week_01/id_28/LeetCode_104_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
return Dfs(0, root);
}
private:
int Dfs(int level, TreeNode *node) {
return node ? max(Dfs(level + 1, node->left), Dfs(level + 1, node->right)) : level;
}
};
17 changes: 17 additions & 0 deletions Week_01/id_28/LeetCode_111_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
if (!root) return 0;
int l = minDepth(root->left), r = minDepth(root->right);
return 1 + (min(l, r) ? min(l, r) : max(l, r));
}
};
24 changes: 24 additions & 0 deletions Week_01/id_28/LeetCode_15_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> vv;
if (nums.size() < 3) return vv;
sort(nums.begin(), nums.end());
for (int i = 0; i < (nums.size() - 2); i++) {
if ((i > 0) && (nums[i] == nums[i - 1])) continue;
int l = i + 1, r = nums.size() - 1;
while (l < r) {
int sum = nums[i] + nums[l] + nums[r];
if (sum < 0) l++;
else if (sum > 0) r--;
else {
vv.push_back(vector<int> {nums[i], nums[l], nums[r]});
while ((l < (nums.size() - 1)) && nums[l] == nums[l + 1]) l++;
while ((r > 0) && nums[r] == nums[r - 1]) r--;
l++; r--;
}
}
}
return vv;
}
};
26 changes: 26 additions & 0 deletions Week_01/id_28/LeetCode_174_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
int r = dungeon.size();
int c = dungeon[0].size();

int dp[r][c];
dp[r - 1][c - 1] = dungeon[r - 1][c - 1] >= 0 ? 1 : 1 - (dungeon[r - 1][c - 1]);

for (int i = r - 1; i >= 0; i--) {
for (int j = c - 1; j >= 0; j--) {
if ((i == r - 1) && (j == c - 1)) continue;
if (i == r - 1) {
dp[i][j] = dp[i][j + 1] - dungeon[i][j];
} else if (j == c - 1) {
dp[i][j] = dp[i + 1][j] - dungeon[i][j];
} else {
dp[i][j] = min(dp[i][j + 1], dp[i + 1][j]) - dungeon[i][j];
}
if (dp[i][j] < 1) dp[i][j] = 1;
}
}

return dp[0][0];
}
};
9 changes: 9 additions & 0 deletions Week_01/id_28/LeetCode_189_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution {
public:
void rotate(vector<int>& nums, int k) {
k %= nums.size();
reverse(nums.begin(), nums.end());
reverse(nums.begin(), nums.begin() + k);
reverse(nums.begin() + k, nums.end());
}
};
22 changes: 22 additions & 0 deletions Week_01/id_28/LeetCode_21_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (!l1) return l2;
if (!l2) return l1;
if (l1->val <= l2->val) {
l1->next = mergeTwoLists(l1->next, l2);
return l1;
} else {
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}
};
18 changes: 18 additions & 0 deletions Week_01/id_28/LeetCode_236_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (!root || root == p || root == q) return root;
TreeNode* l = lowestCommonAncestor(root->left, p, q);
TreeNode* r = lowestCommonAncestor(root->right, p, q);
return !l ? r : (!r ? l : root);
}
};
19 changes: 19 additions & 0 deletions Week_01/id_28/LeetCode_242_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
bool isAnagram(string s, string t) {
unordered_map<char, int> ms;
unordered_map<char, int> mt;

for (const auto& c : s) ms[c] += 1;

for (const auto& c : t) mt[c] += 1;

for (const auto& e : ms)
if (e.second != mt[e.first]) return false;

for (const auto& e : mt)
if (e.second != ms[e.first]) return false;

return true;
}
};
18 changes: 18 additions & 0 deletions Week_01/id_28/LeetCode_24_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if (!head || !head->next) return head;
ListNode *n = head->next;
head->next = swapPairs(n->next);
n->next = head;
return n;
}
};
28 changes: 28 additions & 0 deletions Week_01/id_28/LeetCode_257_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> v;
string s = "";
_dfs(root, s, v);
return v;
}
private:
void _dfs(TreeNode* root, string& str, vector<string>& vec) {
if (!root) return;
if (!str.empty()) str += "->";
str += to_string(root->val);
if (!root->left && !root->right) vec.push_back(str);
string clone = str;
_dfs(root->left, clone, vec);
_dfs(root->right, str, vec);
}
};
10 changes: 10 additions & 0 deletions Week_01/id_28/LeetCode_26_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int i = 0;
for (int j = 1; j < nums.size(); j++)
if (nums[i] != nums[j])
nums[++i] = nums[j];
return nums.size() ? ++i: 0;
}
};
19 changes: 19 additions & 0 deletions Week_01/id_28/LeetCode_42_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
int trap(vector<int>& height) {
int sum = 0;
stack<int> st;
for (int i = 0; i < height.size(); i++) {
while (!st.empty() && height[st.top()] < height[i]) {
int top = st.top();
st.pop();
if (st.empty()) break;
int w = i - st.top() - 1;
int h = min(height[i], height[st.top()]) - height[top];
sum += w * h;
}
st.push(i);
}
return sum;
}
};
11 changes: 11 additions & 0 deletions Week_01/id_28/LeetCode_441_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public:
int arrangeCoins(int n) {
int low = 1, high = n;
while (low < high) {
long mid = low + (high - low + 1) / 2;
((mid + 1) * mid / 2 <= n) ? (low = mid) : (high = mid - 1);
}
return high;
}
};
16 changes: 16 additions & 0 deletions Week_01/id_28/LeetCode_49_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, vector<string>> mp;
for (string s : strs) {
string t = s;
sort(t.begin(), t.end());
mp[t].push_back(s);
}
vector<vector<string>> anagrams;
for (auto p : mp) {
anagrams.push_back(p.second);
}
return anagrams;
}
};
10 changes: 10 additions & 0 deletions Week_01/id_28/LeetCode_50_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution {
public:
double myPow(double x, int n) {
if (n == -1) return 1 / x;
if (n == 0) return 1;
if (n == 1) return x;
if (n & 1) return myPow(x * x, n / 2) * (n < 0 ? 1 / x : x);
else return myPow(x * x, n / 2);
}
};
20 changes: 20 additions & 0 deletions Week_01/id_28/LeetCode_783_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int res = INT_MAX, pre = -1;
int minDiffInBST(TreeNode* root) {
if (root->left) minDiffInBST(root->left);
if (pre >= 0) res = min(res, root->val - pre);
pre = root->val;
if (root->right) minDiffInBST(root->right);
return res;
}
};
29 changes: 29 additions & 0 deletions Week_01/id_28/LeetCode_84_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int max = 0;
stack<pair<int, int>> sp;
for (int i = 0; i < heights.size(); i++) {
if (sp.empty() || sp.top().first < heights[i]) {
sp.push(pair<int, int>(heights[i], i));
} else {
int l = 0;
while (!sp.empty() && sp.top().first >= heights[i]) {
pair<int, int> top = sp.top();
l = top.second;
int size = (i - top.second) * top.first;
if (size > max) max = size;
sp.pop();
}
sp.push(pair<int, int>(heights[i], l));
}
}
while (!sp.empty()) {
pair<int, int> top = sp.top();
int size = (heights.size() - top.second) * top.first;
if (size > max) max = size;
sp.pop();
}
return max;
}
};
7 changes: 7 additions & 0 deletions Week_01/id_28/LeetCode_88_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
while (n)
m > 0 && nums1[m-1] > nums2[n-1] ? nums1[n + m] = nums1[--m] : nums1[n + m] = nums2[--n];
}
};
Loading