摘要: 122.买卖股票的最佳时机II 点击查看代码 class Solution { public: int maxProfit(vector<int>& prices) { int result = 0; for(int j = 1; j < prices.size(); ++j) { if(price 阅读全文
posted @ 2025-03-11 20:52 coder小杰 阅读(19) 评论(0) 推荐(0)
摘要: 分发饼干 点击查看代码 class Solution { public: int findContentChildren(vector<int>& g, vector<int>& s) { sort(g.begin(), g.end()); sort(s.begin(), s.end()); int 阅读全文
posted @ 2025-03-11 20:47 coder小杰 阅读(15) 评论(0) 推荐(0)
摘要: 491.递增子序列 解法一:使用unordered_set进行树层去重 点击查看代码 class Solution { public: vector<vector<int>> result; vector<int> path; void backtracking(vector<int>& nums, 阅读全文
posted @ 2025-03-08 00:09 coder小杰 阅读(23) 评论(0) 推荐(0)
摘要: 93.复原IP地址 点击查看代码 class Solution { public: vector<string> result; //检查切割下来的区间[startIndex, endIndex]是否为合法IP字段 bool isValidIP(string &s, int startIndex, 阅读全文
posted @ 2025-03-06 22:12 coder小杰 阅读(11) 评论(0) 推荐(0)
摘要: 组合总和 点击查看代码 class Solution { public: vector<vector<int>> result; vector<int> path; void backtracking(vector<int>& candidates, int &target, int sum, in 阅读全文
posted @ 2025-03-05 23:23 coder小杰 阅读(17) 评论(0) 推荐(0)
摘要: 77.组合 解法一:不隐藏回溯 点击查看代码 class Solution { public: vector<vector<int>> result; vector<int> path; void backtracking(int n, int k, int startIndex) { if(pat 阅读全文
posted @ 2025-03-05 12:38 coder小杰 阅读(17) 评论(0) 推荐(0)
摘要: 701.二叉搜索树中的插入操作 点击查看代码 class Solution { public: void preOrder(TreeNode *root, TreeNode *parent, int val) { if(root == nullptr) { TreeNode *newNode = n 阅读全文
posted @ 2025-03-02 22:47 coder小杰 阅读(20) 评论(0) 推荐(0)
摘要: 530.二叉搜索树的最小绝对差 点击查看代码 class Solution { public: int result = INT_MAX; TreeNode *pre = nullptr; //每次处理完当前节点后,将当前节点更新为pre节点 void inOrder(TreeNode* root) 阅读全文
posted @ 2025-03-02 17:35 coder小杰 阅读(12) 评论(0) 推荐(0)
摘要: 654.最大二叉树 点击查看代码 class Solution { public: //找到[left, right)子树内的根节点,并与其左子树和右子树进行连接,最后返回子树的根节点 TreeNode *searchRoot(vector<int> &nums, int left, int rig 阅读全文
posted @ 2025-03-01 14:15 coder小杰 阅读(15) 评论(0) 推荐(0)
摘要: 513.找树左下角的值 解法一:前序遍历 点击查看代码 class Solution { public: int maxDepth = 0; int result; void preOrder(TreeNode *root, int depth) { if(root == nullptr) retu 阅读全文
posted @ 2025-02-28 16:50 coder小杰 阅读(15) 评论(0) 推荐(0)