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
30 changes: 30 additions & 0 deletions Week_01/id_41/LeetCode_101_41.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* @lc app=leetcode id=101 lang=cpp
*
* [101] Symmetric Tree
*/
/**
* 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 root == NULL || isSymmetricHelp(root->left, root->right);
}
bool isSymmetricHelp(TreeNode* left, TreeNode* right) {
if (left == NULL || right == NULL) {
return left == right;
}
if (left->val != right->val) {
return false;
}
return isSymmetricHelp(left->left, right->right) && isSymmetricHelp(left->right, right->left);
}
};

22 changes: 22 additions & 0 deletions Week_01/id_41/LeetCode_104_41.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* @lc app=leetcode id=104 lang=cpp
*
* [104] Maximum Depth of Binary Tree
*/
/**
* 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 (root == nullptr) ? 0 : max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
};
26 changes: 26 additions & 0 deletions Week_01/id_41/LeetCode_111_41.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* @lc app=leetcode id=111 lang=cpp
*
* [111] Minimum Depth of Binary Tree
*/
/**
* 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 == NULL)
return 0;
int left = minDepth(root->left);
int right = minDepth(root->right);
return (left == 0 || right == 0) ? (left + right + 1) : min(left, right) + 1;
}
};
26 changes: 26 additions & 0 deletions Week_01/id_41/LeetCode_174_41.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* @lc app=leetcode id=174 lang=cpp
*
* [174] Dungeon Game
*/
class Solution {
public:
int calculateMinimumHP(vector<vector<int> > &dungeon) {
int M = dungeon.size();
int N = dungeon[0].size();
// hp[i][j] represents the min hp needed at position (i, j)
// Add dummy row and column at bottom and right side
vector<vector<int> > hp(M + 1, vector<int>(N + 1, INT_MAX));
hp[M][N - 1] = 1;
hp[M - 1][N] = 1;
for (int i = M - 1; i >= 0; i--) {
for (int j = N - 1; j >= 0; j--) {
int need = max(1, min(hp[i + 1][j], hp[i][j + 1])) - dungeon[i][j];
hp[i][j] = need <= 0 ? 1 : need;
}
}
return hp[0][0];
}
};


38 changes: 38 additions & 0 deletions Week_01/id_41/LeetCode_257_41.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* @lc app=leetcode id=257 lang=cpp
*
* [257] Binary Tree Paths
*/
/**
* 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> answer;
if (root != nullptr) {
searchBT(root, "", answer);
}
return answer;
}

private:
void searchBT(TreeNode* root, string path, vector<string>& answer) {
if (root->left == nullptr && root->right == nullptr) {
answer.push_back(path + to_string(root->val));
}
if (root->left != nullptr) {
searchBT(root->left, path + to_string(root->val) + "->", answer);
}
if (root->right != nullptr) {
searchBT(root->right, path + to_string(root->val) + "->", answer);
}
}
};

20 changes: 20 additions & 0 deletions Week_01/id_41/LeetCode_263_41.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* @lc app=leetcode id=263 lang=cpp
*
* [263] Ugly Number
*/
class Solution
{
public:
bool isUgly(int num)
{
for (int i = 2; i < 6 && (num != 0); i++)
{
while (num % i == 0)
{
num /= i;
}
}
return num == 1;
}
};
21 changes: 21 additions & 0 deletions Week_01/id_41/LeetCode_26_41.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* @lc app=leetcode id=26 lang=cpp
*
* [26] Remove Duplicates from Sorted Array
*/
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int i = 0;
for (int j = 0; j < nums.size(); j++)
{
if (!i || nums[j] > nums[i-1])
{
nums[i] = nums[j];
i++;
}
}
return i;
}
};

17 changes: 17 additions & 0 deletions Week_01/id_41/LeetCode_338_41.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* @lc app=leetcode id=338 lang=cpp
*
* [338] Counting Bits
*/
class Solution {
public:
vector<int> countBits(int num) {
vector<int> bits(num + 1, 0);
for (int i = 1; i <= num; ++i)
{
bits[i] += bits[i & (i - 1)] + 1;
}
return bits;
}
};

17 changes: 17 additions & 0 deletions Week_01/id_41/LeetCode_441_41.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* @lc app=leetcode id=441 lang=cpp
*
* [441] Arranging Coins
*/
class Solution {
public:
int arrangeCoins(int n) {
int i = 1;
while (n >= i) {
n -= i;
i++;
}
return i - 1;
}
};

20 changes: 20 additions & 0 deletions Week_01/id_41/LeetCode_50_41.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* @lc app=leetcode id=50 lang=cpp
*
* [50] Pow(x, n)
*/
class Solution
{
public:
double myPow(double x, int n)
{
if (n == 0)
return 1;
if (n < 0)
{
n = -n;
x = 1 / x;
}
return (n & 1) ? pow(x * x, n / 2) * x : pow(x * x, n / 2);
}
};
31 changes: 31 additions & 0 deletions Week_01/id_41/LeetCode_783_41.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* @lc app=leetcode id=783 lang=cpp
*
* [783] Minimum Distance Between BST Nodes
*/
/**
* 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;
int preVal = -1;
int minDiffInBST(TreeNode *root)
{
if (root->left != NULL)
minDiffInBST(root->left);
if (preVal >= 0)
res = min(res, root->val - preVal);
preVal = root->val;
if (root->right != NULL)
minDiffInBST(root->right);
return res;
}
};
28 changes: 28 additions & 0 deletions Week_01/id_41/LeetCode_938_41.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* @lc app=leetcode id=938 lang=cpp
*
* [938] Range Sum of BST
*/
/**
* 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 rangeSumBST(TreeNode *root, int L, int R)
{
if (root == nullptr)
return 0;
if (root->val > R)
return rangeSumBST(root->left, L, R);
if (root->val < L)
return rangeSumBST(root->right, L, R);
return root->val + rangeSumBST(root->left, L, R) + rangeSumBST(root->right, L, R);
}
};
2 changes: 2 additions & 0 deletions Week_01/id_41/NOTE.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# 学习笔记
递归代码看着的确很清爽。
C写起来会很累,CPP的vector<vector<int>>二维数组比C动态分配数组来通过指针计算方便很多。
Binary file added algo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.