随笔分类 - LeetCode刷题
记录LeetCode刷题
摘要:图解找递推公式 const int N = 20; class Solution { public: int dp[N]; int numTrees(int n) { dp[0] = 1; for (int i = 1; i <= n; i ++) for (int j = 0; j <= i -
阅读全文
摘要:二叉搜索树 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉搜索树 /** * Definition for a binary tree node. * struct TreeNode { *
阅读全文
摘要:const int N = 60; class Solution { public: int dp[N]; int integerBreak(int n) { dp[2] = 1; for (int i = 3; i <= n; i ++) for (int j = 1; j < i; j ++)
阅读全文
摘要:dfs(超时) class Solution { public: int res = 0; void dfs(int x, int y, int m, int n) { if (x == m && y == n) res ++; if (x + 1 <= m) dfs(x + 1, y, m, n)
阅读全文
摘要:动态规划 const int N = 1000; class Solution { public: int dp[N]; int minCostClimbingStairs(vector<int>& cost) { dp[0] = cost[0]; dp[1] = cost[1]; for (int
阅读全文
摘要:动态规划 const int N = 50; class Solution { public: int dp[N]; int climbStairs(int n) { dp[0] = 1; dp[1] = 1; for (int i = 2; i <= n; i ++) dp[i] = dp[i -
阅读全文
摘要:动态规划 const int N = 40; class Solution { public: int dp[N]; int fib(int n) { dp[1] = 1; for (int i = 2; i <= n; i ++) dp[i] = dp[i - 1] + dp[i - 2]; re
阅读全文
摘要://设正整数值为x,那么负整数绝对值为sum - x //target = x - (sum - x) //x = (target + sum) / 2 const int N = 1010; class Solution { public: int dp[N]; int findTargetSum
阅读全文
摘要:const int N = 3010; class Solution { public: int dp[N]; int lastStoneWeightII(vector<int>& stones) { int sum = 0; for (int i = 0; i < stones.size(); i
阅读全文
摘要:01背包 const int N = 20010; class Solution { public: int dp[N]; bool canPartition(vector<int>& nums) { int sum = 0; for (int i = 0; i < nums.size(); i +
阅读全文
摘要:后序遍历 class Solution { public: int dfs(TreeNode* node) { if (node == nullptr) return 0; if (node->left == nullptr && node->right != nullptr) { return 1
阅读全文
摘要:class Solution { public: int maxDepth(Node* root) { if (root == nullptr) return 0; int depth = 0; for (int i = 0; i < root->children.size(); i ++) { d
阅读全文
摘要:前序遍历解法 前序遍历求二叉树深度 class Solution { public: int res = 0; void dfs(TreeNode* root, int depth) { res = res > depth ? res : depth; if (root->left == nullp
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), rig
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), rig
阅读全文
摘要:二叉树种类 满二叉树 完全二叉树(底层连续) 二叉搜索树(节点元素有一定顺序) 平衡二叉搜索树(左子树与右子树高度差绝对值小于) 存储方式 链式存储 线式存储 二叉树的遍历 深度优先遍历 前序遍历 中左右 中序遍历 左中右 后序遍历 左右中 广度优先遍历 层序遍历 迭代法 LeetCode 144
阅读全文
摘要:class Solution { public: int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) { unordered_map<int, int> ma
阅读全文
摘要:class Solution { public: vector<vector<int>> fourSum(vector<int>& nums, int target) { vector<vector<int>> res; sort(nums.begin(), nums.end()); for (in
阅读全文
摘要:class Solution { public: bool canConstruct(string ransomNote, string magazine) { int record[26] ={0}; //默认值为0 if (magazine.size() < ransomNote.size())
阅读全文
摘要:class Solution { public: vector<vector<int>> res; vector<vector<int>> threeSum(vector<int>& nums) { sort(nums.begin(), nums.end()); for (int i = 0; i
阅读全文