随笔分类 -  leetcode

摘要:``` class Solution { public: vector> res; vector path; bool st[10]; void dfs(vector nums,int u) { if(u==nums.size()) { res.push_back(path); return; } 阅读全文
posted @ 2023-06-10 19:57 穿过雾的阴霾 阅读(4) 评论(0) 推荐(0) 编辑
摘要:``` class Solution { public: unordered_map cnt; vector> res; vector path; vector> subsetsWithDup(vector& nums) { for(auto i:nums) cnt[i]++; dfs(-10);/ 阅读全文
posted @ 2023-06-07 21:04 穿过雾的阴霾 阅读(4) 评论(0) 推荐(0) 编辑
摘要:``` class Solution { public: vector> res; vector> combinationSum2(vector& candidates, int target) { sort(candidates.begin(),candidates.end()); dfs(can 阅读全文
posted @ 2023-06-07 20:37 穿过雾的阴霾 阅读(4) 评论(0) 推荐(0) 编辑
摘要:``` class Solution { public: vector> res; vector> combinationSum(vector& candidates, int target) { dfs(candidates,0,target); return res; } vector path 阅读全文
posted @ 2023-06-07 20:09 穿过雾的阴霾 阅读(7) 评论(0) 推荐(0) 编辑
摘要:```c class Solution { public: void dfs(TreeNode* root,int &sum)//从大到小遍历所有节点 { if(!root) return; dfs(root->right,sum); sum+=root->val; root->val=sum; d 阅读全文
posted @ 2023-06-04 14:16 穿过雾的阴霾 阅读(3) 评论(0) 推荐(0) 编辑
摘要:``` class Solution { public: vector res; int cnt=0; int find(TreeNode* root,int val)//返回当前子树值为val的个数 { if(!root) return 0; return find(root->left,val) 阅读全文
posted @ 2023-06-03 20:30 穿过雾的阴霾 阅读(4) 评论(0) 推荐(0) 编辑
摘要:```c class Solution { public: vector path1,path2; bool dfs(TreeNode* root,TreeNode* p,vector& path) { if(!root) return false; if(root==p||dfs(root->le 阅读全文
posted @ 2023-06-02 14:39 穿过雾的阴霾 阅读(4) 评论(0) 推荐(0) 编辑
摘要:```c class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(p->valval&&q->valval) return lowestCommonA 阅读全文
posted @ 2023-06-02 12:40 穿过雾的阴霾 阅读(2) 评论(0) 推荐(0) 编辑
摘要:```c class Solution { public: vector res; unordered_map hashmap;//记录每一个子树出现的次数 string dfs(TreeNode* root) { if(!root) return ""; string str=""; str+=t 阅读全文
posted @ 2023-05-28 14:47 穿过雾的阴霾 阅读(9) 评论(0) 推荐(0) 编辑
摘要:# 思路1 ``` class Solution { public: void flatten(TreeNode* root) { while(root) { auto p=root->left; if(p)//找到左儿子的右链 { while(p->right) p=p->right; //将右链 阅读全文
posted @ 2023-05-27 10:30 穿过雾的阴霾 阅读(4) 评论(0) 推荐(0) 编辑
摘要:``` class Solution { public: vector dfs(TreeNode* root)//依次返回是否是二叉搜索树,最大值最小值 { vector res{1,root->val,root->val}; if(root->left) { auto l=dfs(root->le 阅读全文
posted @ 2023-05-24 14:39 穿过雾的阴霾 阅读(6) 评论(0) 推荐(0) 编辑
摘要:``` class Solution { public: int countNodes(TreeNode* root) { if(!root) return 0; auto l=root->left,r=root->right; int x=1,y=1;//记录左右两边层数 while(l) l=l 阅读全文
posted @ 2023-05-24 14:15 穿过雾的阴霾 阅读(7) 评论(0) 推荐(0) 编辑
摘要:``` class Solution { public: vector dfs(int l,int r)//返回以n为根节点的所有可能子树 { vector res; if(l>r) return {NULL}; for(int k=l;k left=dfs(l,k-1); vector right 阅读全文
posted @ 2023-05-23 10:50 穿过雾的阴霾 阅读(7) 评论(0) 推荐(0) 编辑
摘要:``` class Solution { public: vector> res; void bfs(TreeNode* root) { queue q; q.push(root); int cnt=0; while(!q.empty()) { vector level; int len=q.siz 阅读全文
posted @ 2023-05-22 13:24 穿过雾的阴霾 阅读(4) 评论(0) 推荐(0) 编辑
摘要:``` class Solution { public: vector> res; void bfs(TreeNode* root) { queue q; q.push(root); int last=0; while(!q.empty()) { vector level; int len=q.si 阅读全文
posted @ 2023-05-22 13:15 穿过雾的阴霾 阅读(6) 评论(0) 推荐(0) 编辑
摘要:``` /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), 阅读全文
posted @ 2023-05-21 15:23 穿过雾的阴霾 阅读(6) 评论(0) 推荐(0) 编辑
摘要:``` class Solution { public: vector inorderTraversal(TreeNode* root) { vector res; stack st; while(root||st.size()) { while(root) { st.push(root); roo 阅读全文
posted @ 2023-05-21 15:05 穿过雾的阴霾 阅读(7) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: string longestPalindrome(string s) { string res; int n=s.size(); for(int i=0;i<n;i++) { //长度是奇数 int l=i-1,r=i+1; while(l>=0&& 阅读全文
posted @ 2023-05-16 20:25 穿过雾的阴霾 阅读(6) 评论(0) 推荐(0) 编辑
摘要:可以直接分类讨论,分别枚举第一个房屋偷或不偷的情况,最后再取极值 不偷第一家 f[i] 代表前 i 个房屋,偷第 i 个, 且不偷第一家的最大值 g[i] 代表前 i 个房屋,不偷第 i 个, 且不偷第一家的最大值 偷第一家 f[i] 代表前 i 个房屋,偷第 i 个, 且偷第一家的最大值 g[i] 阅读全文
posted @ 2023-05-15 14:00 穿过雾的阴霾 阅读(19) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: int maxProfit(vector<int>& prices) { int n=prices.size(); vector<int> f(n+1),g(n+1); //预处理f,f[i]表示前i天交易的最大值 for(int i=1,min_p 阅读全文
posted @ 2023-05-14 15:04 穿过雾的阴霾 阅读(14) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示