摘要: 理论基础 Dynamic Programming,当前状态由上一状态推导而来。 FIB 斐波那契 1.初始化数组的时候需要分配数组大小; 2.如果没有分配,使用push_back是安全的; 题解 class Solution { public: int fib(int n) { if(n<=1) r 阅读全文
posted @ 2025-12-08 00:22 FAfa_C++ 阅读(13) 评论(0) 推荐(0)
摘要: 用最少数量的箭引爆气球 思路 如果当前数组的左区间没有覆盖前一个数组的右区间,就需要新的箭来击穿。不然就更新新的右区间。 class Solution { static bool cmd(const vector<int>&a,vector<int>&b){ if(a[0]==b[0]){ retu 阅读全文
posted @ 2025-12-07 19:24 FAfa_C++ 阅读(15) 评论(0) 推荐(0)
摘要: 加油站 抽象,想到了一些,但是思路并没有缕清楚。 class Solution { public: int canCompleteCircuit(vector& gas, vector& cost) { int rest; for (int i = 0; i < cost.size(); i++) 阅读全文
posted @ 2025-12-06 00:43 FAfa_C++ 阅读(13) 评论(0) 推荐(0)
摘要: 买卖股票的最佳时机 问题理解 整数数组的下标表示股票某天的价格,需要返回最大利润。题目多少有点离谱了,在同一天多次买卖该股票,但是特定日期的价格是确定的。 class Solution { public: int maxProfit(vector<int>& prices) { int result 阅读全文
posted @ 2025-12-05 23:07 FAfa_C++ 阅读(15) 评论(0) 推荐(0)
摘要: 分发饼干 代码 class Solution { public: int findContentChildren(vector<int>& g, vector<int>& s) { sort(g.begin(), g.end()); sort(s.begin(), s.end()); int ind 阅读全文
posted @ 2025-12-04 00:14 FAfa_C++ 阅读(15) 评论(0) 推荐(0)
摘要: Hello World 阅读全文
posted @ 2025-11-30 12:25 FAfa_C++ 阅读(12) 评论(0) 推荐(0)
摘要: 非递减子序列 问题描述 给了一个数组,要求给出其所有长度>=2的非递减子序列。 思路 压入结果的条件是path.size()>=2,回溯过程结束的条件是移动到了边上startIndex>=num.size() 在树中,非递减序列,要求压入的元素必须比之前压入的大:if(path.empty()||n 阅读全文
posted @ 2025-11-29 20:35 FAfa_C++ 阅读(82) 评论(1) 推荐(0)
摘要: 回溯练习:再有重复元素和无重复元素的数组中分别找到和为target的子集,以及给出指定字符串的回文子字符串。 阅读全文
posted @ 2025-11-28 21:40 FAfa_C++ 阅读(16) 评论(0) 推荐(0)
摘要: 复原IP地址 题目理解 1.给出的是字符串,IP地址在【0,255】之间,字符串转数字; 2.0可以单独出现,但是不能跟数字出现; 3.字符串中要插入'.'; 4.字符串的大小范围在4~12之间; 5.不能有除数字外的字母; 思路 首先确定答案类型:vector<string> res,一个用来存单 阅读全文
posted @ 2025-11-28 21:37 FAfa_C++ 阅读(36) 评论(1) 推荐(0)
摘要: 回溯理论 什么是回溯 回溯,顾名思义,返回溯源,记录当前节点后返回前一节点继续的过程。本质上是一种罗列所有情况的穷举搜索。 递归 递归,函数间接或者直接调用自身,回到最初最简单的情况。目前的情况归根结底就是一棵树的情况。 回溯与递归 为什么说回溯常常伴随递归?递归是把一棵大二叉树返回到一个最基本的三 阅读全文
posted @ 2025-11-26 15:44 FAfa_C++ 阅读(51) 评论(0) 推荐(0)