LeetCode 797. 所有可能的路径 原题目:https://leetcode-cn.com/problems/all-paths-from-source-to-target/思路:使用BFS + 回溯算法代码:class Solution { vector<vector<int>> ans; vector<int> tmp; void bfs(vector<vector<int>>& graph,int index){ ..
LeetCode 1365. 有多少小于当前数字的数字 原题目:https://leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number/思路:采用计数排序的方法代码:class Solution {public: vector<int> smallerNumbersThanCurrent(vector<int>& nums) { vector<int> cnt(10..
LeetCode 143. 重排链表 原题目:https://leetcode-cn.com/problems/reorder-list/思路:使用vector存储节点,然后使用双指针进行重新连接,最后注意最后的节点的next置为空代码:class Solution {public: void reorderList(ListNode *head) { if (head == nullptr) { return; } vector&l..
重定向 --- 同时在中断和文件进行输出 重定向 --- 同时在中断和文件进行输出使用重定向技术,在模型训练的过程中同时在中断和日志文件中输出我们的结果。代码:class Logger(object): def __init__(self, filename='default.log', stream=sys.stdout): self.terminal = stream self.log = open(filename, 'a') def write(self, message):..
LeetCode 977. 有序数组的平方 原题目:https://leetcode-cn.com/problems/squares-of-a-sorted-array/思路:使用双指针,从后向前加元素。代码:class Solution {public: vector<int> sortedSquares(vector<int>& A) { int i=0,j=A.size()-1,index = A.size()-1; vector<in..
LeetCode 116. 填充每个节点的下一个右侧节点指针 原题目:https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/思路:采用BFS,使用size记录该层的个数,然后修改next指针就可以了代码class Solution {public: Node* connect(Node* root) { if(root == nullptr) return root; queue<Node*&..
LeetCode 1002. 查找常用字符 原题目:https://leetcode-cn.com/problems/find-common-characters/思路:使用哈希表,每次遍历完一个字符串后,取最小的次数代码:class Solution {public: vector<string> commonChars(vector<string>& A) { vector<int> m(26,INT_MAX); vector<..
LeetCode 530. 二叉搜索树的最小绝对差 原题目:https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/思路:中序遍历,求解相邻接点的差值绝对值的最小值代码:class Solution { bool flag; int ans,pre; void dfs(TreeNode* root){ if(root == nullptr) return; dfs(root->left)..
LeetCode 75. 颜色分类 原题目:https://leetcode-cn.com/problems/sort-colors/思路:使用双指针,l记录当前左边到的位置,r记录右边到的位置为0,移动到前面。l++,cur++;为2移动到后面。r--。(此时不可以cur++,因为nums[r]使我们没有碰到过得元素)1时进行下一个元素的判断。cur++代码:class Solution {public: void sortColors(vector<int>& num..
LeetCode 834. 树中距离之和 原题目:https://leetcode-cn.com/problems/sum-of-distances-in-tree/思路:采用树形动态规划的思想。对于每一个节点来说,所有节点到他的距离之和的状态转移方程为:其中dp[v]代表以v为根的所有节点到他的距离,sz[v]表示已v为根的子树节点的数量。当一次遍历完,得到了所有节点的dp时,我们不需要以另一个节点为根重新遍历,只需要对树结构进行变化(官方解答的视频),所以每一次做变化我们只需要变换dp[u],sz[u],dp[v],s.
LeetCode 771. 宝石与石头 原题目:https://leetcode-cn.com/problems/jewels-and-stones/思路:构造哈希表代码:class Solution {public: int numJewelsInStones(string J, string S) { map<char,int> h; for(char& c:J) h[c] = 1; int sum = 0; for(c..
跑深度学习模型进行的日志输出 跑深度学习模型进行的日志输出构造Logger类进行输出重定向。在write函数中,使用两个函数使得命令行和日志都有输出class Logger(object): def __init__(self, filename='default.log', stream=sys.stdout): self.terminal = stream self.log = open(filename, 'a') def write(self, message): .
LeetCode 79. 单词搜索 原题目:https://leetcode-cn.com/problems/word-search/思路:简单的回溯算法,开一个数组记录访问过得节点代码:class Solution { bool flag=false; void dfs(vector<vector<char>>& board,string& word,vector<vector<int>>& v,int x,int y,in..
LeetCode 60. 第k个排列 原题目:https://leetcode-cn.com/problems/permutation-sequence/思路:康拓展开代码:class Solution {public: string getPermutation(int n, int k) { vector<char> chs={'1','2','3','4','5','6','7','8','9'}; const int factor[]={1,1,2,6,24,120,720,5040..
LeetCode 235. 二叉搜索树的最近公共祖先 原题目:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/思路:利用二叉搜索树的性质代码:class Solution {public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(root->val > p->va..
LeetCode 501. 二叉搜索树中的众数 原题目:https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/思路:对BST采用中序遍历,如果当前节点的值不等于前一节点的值,那么就对其进行判断,看看前一节点值得次数是否大于了maxn。分情况进行处理。细节:记得对中序遍历的第一个节点做初始化,中序遍历的最后一种节点在递归过程中是不会进行判断的(因为其后面没有节点和他进行比较),所以在main函数中,要对这一情况做单独的判断。代码:class S..
差分进化算法 1 简介最优化方法分为传统优化方法和启发式方法,传统的优化算法大多数都是利用目标函数的导数求解,而启发式优化方法以放生算法为主,通过启发式搜索策略实现求解优化,启发式搜索算法不要求目标函数连续,可微等信息,具有较好的全局寻优能力。是一种用于最优化问题的后设启发式算法,本质上是一种基于实数编码的具有保优思想的贪婪遗传算法。在AI领域,演化算法是演化计算的一个重要分支,基于群体的元启发式优化算法,具有自适应、自搜索、自组织和隐并行性等特点。目前演化算法广泛应用于求解无约束函数优化、约束函数优化、组合
LeetCode 896. 单调数列 原题目:https://leetcode-cn.com/problems/monotonic-array/思路:使用两个flag(a和b)来表明里面存在增和减的情况,如果两者同时出现,那么就不单调代码:class Solution {public: bool isMonotonic(vector<int>& A) { if(A.size() < 3) return true; int a=0,b=0; ..
LeetCode 617. 合并二叉树 原题目:https://leetcode-cn.com/problems/merge-two-binary-trees/思路:构造新的节点,判断递归的终止条件就可以代码:class Solution {public: TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { if(t1 ==nullptr) return t2; if(t2 == nullptr) return t1; ..