摘要: vector<int> ans; void dfs(TreeNode* now){ if (now==nullptr) return ; dfs(now->left); ans.push_back(now->val); dfs(now->right); } class Solution { publ 阅读全文
posted @ 2021-06-17 22:54 wegret 阅读(27) 评论(0) 推荐(0)
摘要: class Solution { public: int guessNumber(int n) { long long l=1,r=(long long)n; long long mid=((long long)1+n)>>1,temp; while (true){ temp=guess(mid); 阅读全文
posted @ 2021-06-17 22:44 wegret 阅读(25) 评论(0) 推荐(0)
摘要: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), rig 阅读全文
posted @ 2021-06-12 21:12 wegret 阅读(40) 评论(0) 推荐(0)
摘要: 简单地dfs一遍即可。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(n 阅读全文
posted @ 2021-06-12 18:56 wegret 阅读(39) 评论(0) 推荐(0)
摘要: 其实就是一个求LCA的模板问题。 我的实现方法是在p、q上分别打一个标记。然后递归把标记向上传递。当找到一个拥有两个标记的节点,它就是最近公共祖先。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * T 阅读全文
posted @ 2021-06-12 17:57 wegret 阅读(27) 评论(0) 推荐(0)
摘要: 中序遍历得到一个队列。然后找left和right之间的和就行了。 (第一遍理解错了意思。看懂了就不难。) int l,h; int que[20007],last; void dfs(TreeNode* now){ if (now==NULL) return ; dfs(now->left); qu 阅读全文
posted @ 2021-06-12 17:38 wegret 阅读(24) 评论(0) 推荐(0)
摘要: warning: if statement has empty body [-Wempty-body] 可能句末多打了分号。 warning: using the result of an assignment as a condition without parentheses [-Wparent 阅读全文
posted @ 2021-06-12 17:06 wegret 阅读(3205) 评论(0) 推荐(1)
摘要: int d[100007]; class Solution { public: int jump(vector<int>& nums) { memset(d,127,sizeof(d)); d[0]=0; for (int i=0;i<nums.size();i++) for (int j=1;i+ 阅读全文
posted @ 2021-06-11 18:00 wegret 阅读(21) 评论(0) 推荐(0)
摘要: 贪心,$O(n)$。 扫一遍数组。声明一个last存从1出发可以跳到的最远位置。如果可以跳到$i$,那么最远的位置起码可以到$i+nums[i]$。如果扫到最远都到不了的点,就return 0。 class Solution { public: bool canJump(vector<int>& n 阅读全文
posted @ 2021-06-11 17:03 wegret 阅读(40) 评论(0) 推荐(0)
摘要: 贪心。一串数字中取最大值。 class Solution { public: int minPartitions(string n) { char result=0; for (int i=0;i<n.size();i++) result=n[i]>result?n[i]:result; retur 阅读全文
posted @ 2021-06-10 21:32 wegret 阅读(35) 评论(0) 推荐(0)