摘要: 使用智能指针释放在堆上分配的内存(超出作用域就释放) class Entity { private: float x, y; public: Entity(float x,float y):x(x),y(y) { std::cout<<"Created Entity!"<<std::endl; } 阅读全文
posted @ 2023-08-25 16:47 iu本u 阅读(15) 评论(0) 推荐(0)
摘要: 再数据结构内重载它的操作符,一般会先写一个函数,然后再重载符号:返回类型+operator+重载符号(参数){//定义} Vector2 Multity(const Vector2& other)const { return Vector2(x * other.x, y * other.y); } 阅读全文
posted @ 2023-08-25 16:11 iu本u 阅读(18) 评论(0) 推荐(0)
摘要: 递归 TreeNode* dfs(TreeNode* root,TreeNode* p,TreeNode* q){ if(!root)return root;//当发现这个节点已经是叶节点时,要告诉上层 if(root==p||root==q)return root;//当发现是p节点或者q时也要告 阅读全文
posted @ 2023-08-25 14:48 iu本u 阅读(25) 评论(0) 推荐(0)
摘要: 当构造函数只有一个构造函数时,可以直接将参数赋值给类对象 class Entity { public: String m_Name; int m_Age; public: explicit Entity(const String& name) :m_Name(name) ,m_Age(0){} En 阅读全文
posted @ 2023-08-22 20:21 iu本u 阅读(46) 评论(0) 推荐(0)
摘要: 在堆上的类对象或者任何变量,一旦声明将会一直存在,直到手动释放 使用new关键字,使用delete释放;可以显示控制生存期 数组使用delete删除时也要使用delete [ ] a; new关键字作用:1.在堆上分配内存 2.调用类的构造函数 new还能指定分配内存的位置 class Entity 阅读全文
posted @ 2023-08-22 19:40 iu本u 阅读(16) 评论(0) 推荐(0)
摘要: bfs+dp unordered_map<TreeNode* ,int>d,p; queue<pair<TreeNode* ,TreeNode*>>q; int dp(TreeNode* root){ d[root]=p[root]=0; q.push({root,nullptr}); while( 阅读全文
posted @ 2023-08-18 19:11 iu本u 阅读(30) 评论(0) 推荐(0)
摘要: DFS 没有返回值 max在递归时要慎重用引用,因为在回溯时可能不能改变max; 因为节点的值可能有负数,所以最大值从根节点开始,根节点一定是好节点。 int goodNum=0; void dfs(TreNode* root,int max){ if(!root)return ; if(root- 阅读全文
posted @ 2023-07-25 19:31 iu本u 阅读(33) 评论(0) 推荐(0)
摘要: 这道题是考虑的深度优先搜索,使用递归 vecotr和queue入队操作并不相同: vector只能使用push_back(); queue既可以使用push()还可以使用push_back() void FindLeaf(TreeNode* root,vector<int>& v){ if(!roo 阅读全文
posted @ 2023-07-21 13:20 iu本u 阅读(20) 评论(0) 推荐(0)
摘要: 深度优先搜索,递归 maxDepth(TreeNode* root){ if(!root)return 0; return max(maxDepth(root->left),maxDepth(root->right))+1; } 广度优先搜索,队列 queue<TreeNode*>q; q.push 阅读全文
posted @ 2023-07-18 13:53 iu本u 阅读(13) 评论(0) 推荐(0)
摘要: 1尾插法:记录前面的节点,使后面的节点指向前面的节点;记后面的节点为now。因为要不停移动now,使其移动所以要临时记录原来之后的节点。 ListNode* now=slow->next; ListNode* pre=nullptr; while(now){ ListNode* node=now-> 阅读全文
posted @ 2023-07-17 15:27 iu本u 阅读(15) 评论(0) 推荐(0)