diff --git a/Week_01/id_41/LeetCode_101_41.cpp b/Week_01/id_41/LeetCode_101_41.cpp new file mode 100644 index 00000000..b923043c --- /dev/null +++ b/Week_01/id_41/LeetCode_101_41.cpp @@ -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); + } +}; + diff --git a/Week_01/id_41/LeetCode_104_41.cpp b/Week_01/id_41/LeetCode_104_41.cpp new file mode 100644 index 00000000..8061d492 --- /dev/null +++ b/Week_01/id_41/LeetCode_104_41.cpp @@ -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; + } +}; diff --git a/Week_01/id_41/LeetCode_111_41.cpp b/Week_01/id_41/LeetCode_111_41.cpp new file mode 100644 index 00000000..f2460186 --- /dev/null +++ b/Week_01/id_41/LeetCode_111_41.cpp @@ -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; + } +}; diff --git a/Week_01/id_41/LeetCode_174_41.cpp b/Week_01/id_41/LeetCode_174_41.cpp new file mode 100644 index 00000000..e154f14c --- /dev/null +++ b/Week_01/id_41/LeetCode_174_41.cpp @@ -0,0 +1,26 @@ +/* + * @lc app=leetcode id=174 lang=cpp + * + * [174] Dungeon Game + */ +class Solution { +public: + int calculateMinimumHP(vector > &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 > hp(M + 1, vector(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]; + } +}; + + diff --git a/Week_01/id_41/LeetCode_257_41.cpp b/Week_01/id_41/LeetCode_257_41.cpp new file mode 100644 index 00000000..27566968 --- /dev/null +++ b/Week_01/id_41/LeetCode_257_41.cpp @@ -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 binaryTreePaths(TreeNode* root) { + vector answer; + if (root != nullptr) { + searchBT(root, "", answer); + } + return answer; + } + +private: + void searchBT(TreeNode* root, string path, vector& 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); + } + } +}; + diff --git a/Week_01/id_41/LeetCode_263_41.cpp b/Week_01/id_41/LeetCode_263_41.cpp new file mode 100644 index 00000000..828f6b13 --- /dev/null +++ b/Week_01/id_41/LeetCode_263_41.cpp @@ -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; + } +}; diff --git a/Week_01/id_41/LeetCode_26_41.cpp b/Week_01/id_41/LeetCode_26_41.cpp new file mode 100644 index 00000000..77f08c6d --- /dev/null +++ b/Week_01/id_41/LeetCode_26_41.cpp @@ -0,0 +1,21 @@ +/* + * @lc app=leetcode id=26 lang=cpp + * + * [26] Remove Duplicates from Sorted Array + */ +class Solution { +public: + int removeDuplicates(vector& 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; + } +}; + diff --git a/Week_01/id_41/LeetCode_338_41.cpp b/Week_01/id_41/LeetCode_338_41.cpp new file mode 100644 index 00000000..b0184b50 --- /dev/null +++ b/Week_01/id_41/LeetCode_338_41.cpp @@ -0,0 +1,17 @@ +/* + * @lc app=leetcode id=338 lang=cpp + * + * [338] Counting Bits + */ +class Solution { +public: + vector countBits(int num) { + vector bits(num + 1, 0); + for (int i = 1; i <= num; ++i) + { + bits[i] += bits[i & (i - 1)] + 1; + } + return bits; + } +}; + diff --git a/Week_01/id_41/LeetCode_441_41.cpp b/Week_01/id_41/LeetCode_441_41.cpp new file mode 100644 index 00000000..7e62ad9e --- /dev/null +++ b/Week_01/id_41/LeetCode_441_41.cpp @@ -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; + } +}; + diff --git a/Week_01/id_41/LeetCode_50_41.cpp b/Week_01/id_41/LeetCode_50_41.cpp new file mode 100644 index 00000000..bec58ced --- /dev/null +++ b/Week_01/id_41/LeetCode_50_41.cpp @@ -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); + } +}; diff --git a/Week_01/id_41/LeetCode_783_41.cpp b/Week_01/id_41/LeetCode_783_41.cpp new file mode 100644 index 00000000..243fd761 --- /dev/null +++ b/Week_01/id_41/LeetCode_783_41.cpp @@ -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; + } +}; diff --git a/Week_01/id_41/LeetCode_938_41.cpp b/Week_01/id_41/LeetCode_938_41.cpp new file mode 100644 index 00000000..ff24b300 --- /dev/null +++ b/Week_01/id_41/LeetCode_938_41.cpp @@ -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); + } +}; diff --git a/Week_01/id_41/NOTE.md b/Week_01/id_41/NOTE.md index 107ea7d6..f159ed65 100644 --- a/Week_01/id_41/NOTE.md +++ b/Week_01/id_41/NOTE.md @@ -1 +1,3 @@ # 学习笔记 +递归代码看着的确很清爽。 +C写起来会很累,CPP的vector>二维数组比C动态分配数组来通过指针计算方便很多。 diff --git a/algo.png b/algo.png new file mode 100644 index 00000000..8079f7a5 Binary files /dev/null and b/algo.png differ