![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
实习刷题
记录
ZXY-YXZ
学习八股的麻瓜
展开
-
从根到叶的二进制数之和
1022. 从根到叶的二进制数之和 - 力扣(LeetCode) (leetcode-cn.com)递归联乘2这卡了半天。。。。。。class Solution { int sum=0;public: int sumRootToLeaf(TreeNode* root) { if(root==nullptr) return 0; fun(root,0); return sum; } void fun(TreeNode*原创 2021-08-25 21:59:07 · 49 阅读 · 0 评论 -
剑指 Offer 32 - II. 从上到下打印二叉树 II
剑指 Offer 32 - II. 从上到下打印二叉树 II - 力扣(LeetCode) (leetcode-cn.com)利用队列的长度作为内循环。。。。。。class Solution { vector<vector<int>>res;public: vector<vector<int>> levelOrder(TreeNode* root) { if(root==nullptr) return res;原创 2021-08-28 20:39:51 · 42 阅读 · 0 评论 -
面试题 04.02. 最小高度树
面试题 04.02. 最小高度树 - 力扣(LeetCode) (leetcode-cn.com)class Solution {public: TreeNode* sortedArrayToBST(vector<int>& nums) { int low=0; int high=nums.size()-1; TreeNode*head; head=fun(nums,low,high); re原创 2021-08-29 21:48:32 · 41 阅读 · 0 评论 -
53. 最大子序和
53. 最大子序和 - 力扣(LeetCode) (leetcode-cn.com)class Solution {public: int maxSubArray(vector<int>& nums) { int sum=0; int bsum=INT32_MIN; for(int i=0;i<nums.size();i++){ sum+=nums[i]; if(sum&原创 2021-08-30 20:40:10 · 43 阅读 · 0 评论 -
二叉搜索树中的搜索
700. 二叉搜索树中的搜索 - 力扣(LeetCode) (leetcode-cn.com)这题比较简单。class Solution { TreeNode*res;public: TreeNode* searchBST(TreeNode* root, int val) { if(root==nullptr) return nullptr; fun(root,val); return res; } void fun原创 2021-08-24 20:46:13 · 45 阅读 · 0 评论 -
单值二叉树
965. 单值二叉树 - 力扣(LeetCode) (leetcode-cn.com)利用了二叉搜索树最小绝对值思想。定义了preclass Solution { TreeNode*pre=nullptr; bool res=true;public: bool isUnivalTree(TreeNode* root) { if(root==nullptr) return true; fun(root); return res原创 2021-08-25 20:29:13 · 64 阅读 · 0 评论 -
叶子相似的树
872. 叶子相似的树 - 力扣(LeetCode) (leetcode-cn.com)事实证明,即使是全局变量,传入函数中,在函数中改变它的值,最后的值还是没改变。。。。class Solution { vector<int> res1; vector<int> res2;public: bool leafSimilar(TreeNode* root1, TreeNode* root2) { if(root1==nullptr&a原创 2021-08-24 22:16:09 · 45 阅读 · 0 评论 -
剑指 Offer II 052. 展平二叉搜索树
剑指 Offer II 052. 展平二叉搜索树 - 力扣(LeetCode) (leetcode-cn.com)class Solution { TreeNode*pre;public: TreeNode* increasingBST(TreeNode* root) { TreeNode*head=new TreeNode(0); pre=head; fun(root); return head->right;原创 2021-08-29 20:29:24 · 60 阅读 · 0 评论 -
二叉搜索树的范围和
938. 二叉搜索树的范围和 - 力扣(LeetCode) (leetcode-cn.com)class Solution { int sum=0;public: int rangeSumBST(TreeNode* root, int low, int high) { if(root==nullptr) return 0; fun(root,low,high); return sum; } void fun(TreeN原创 2021-08-25 20:21:43 · 46 阅读 · 0 评论 -
面试题 17.12. BiNode
面试题 17.12. BiNode - 力扣(LeetCode) (leetcode-cn.com)class Solution { TreeNode* pre=new TreeNode(0);//记得分配内存public: TreeNode* convertBiNode(TreeNode* root) { if(root==nullptr) return nullptr; TreeNode *temp=pre; fun(root);原创 2021-08-29 22:35:09 · 41 阅读 · 0 评论 -
剑指 Offer 55 - II. 平衡二叉树
剑指 Offer 55 - II. 平衡二叉树 - 力扣(LeetCode) (leetcode-cn.com)class Solution {public: bool isBalanced(TreeNode* root) { if(root==nullptr) return true; if(abs(fun1(root->left)-fun1(root->right))<=1) return (isBalanced(原创 2021-08-28 21:28:45 · 47 阅读 · 0 评论 -
剑指 Offer 55 - I. 二叉树的深度
剑指 Offer 55 - I. 二叉树的深度 - 力扣(LeetCode) (leetcode-cn.com)class Solution {public: int maxDepth(TreeNode* root) { if(root==nullptr) return 0; return dfs(root); } int dfs(TreeNode*root){ if(root==nullptr) return -1;原创 2021-08-28 21:00:07 · 65 阅读 · 0 评论 -
二叉树的堂兄弟节点
993. 二叉树的堂兄弟节点 - 力扣(LeetCode) (leetcode-cn.com)这是我见过最最最简洁的代码。。。。。。class Solution { int xf,xd,yf,yd;public: bool isCousins(TreeNode* root, int x, int y) { dfs(root->left,1,x,y,root->val); dfs(root->right,1,x,y,root->v原创 2021-08-25 21:26:09 · 43 阅读 · 0 评论 -
递增顺序搜索树
897. 递增顺序搜索树 - 力扣(LeetCode) (leetcode-cn.com)太巧妙了吧,只需一个全局变量current。。。。class Solution { TreeNode *current;public: TreeNode* increasingBST(TreeNode* root) { TreeNode *head=new TreeNode(0); current=head; fun(root); re原创 2021-08-25 20:14:14 · 42 阅读 · 0 评论 -
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先 - 力扣(LeetCode) (leetcode-cn.com)class Solution { TreeNode*res=nullptr;public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(root==nullptr) return nullptr; fun(root,p,原创 2021-08-28 21:50:46 · 67 阅读 · 0 评论 -
二叉搜索树节点最小距离
事实证明,第一次做错了,第二次依旧会错。。。。。。。。。。class Solution { TreeNode*pre=nullptr; int val=pow(10,5);public: int minDiffInBST(TreeNode* root) { if(root==nullptr) return 0; fun(root); return val; } void fun(TreeNode*root){原创 2021-08-24 21:58:58 · 43 阅读 · 0 评论 -
二叉树中第二小的节点
671. 二叉树中第二小的节点 - 力扣(LeetCode) (leetcode-cn.com)思想:如果当前节点值大于根节点,返回节点值,孩子不用遍历;否则寻找左右子树中最小值。class Solution {public: int findSecondMinimumValue(TreeNode* root) { return fun(root,root->val); } int fun(TreeNode*root,int val)原创 2021-08-24 20:40:04 · 73 阅读 · 0 评论 -
剑指 Offer 68 - II. 二叉树的最近公共祖先
剑指 Offer 68 - II. 二叉树的最近公共祖先 - 力扣(LeetCode) (leetcode-cn.com)class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(root==nullptr) return nullptr; if(root==p||root==q) return root;原创 2021-08-29 20:03:52 · 58 阅读 · 0 评论 -
剑指 Offer 54. 二叉搜索树的第k大节点
剑指 Offer 54. 二叉搜索树的第k大节点 - 力扣(LeetCode) (leetcode-cn.com)class Solution { vector<int>temp;public: int kthLargest(TreeNode* root, int k) { fun(root); int l=temp.size(); return temp[l-k]; } void fun(TreeNode*原创 2021-08-28 20:45:43 · 51 阅读 · 0 评论 -
对称的二叉树
剑指 Offer 28. 对称的二叉树 - 力扣(LeetCode) (leetcode-cn.com)第二遍做了,还行吧。。。。。class Solution {public: bool isSymmetric(TreeNode* root) { if(root==nullptr) return true; return fun(root->left,root->right); } bool fun(TreeNode*r1,原创 2021-08-25 22:29:56 · 38 阅读 · 0 评论 -
219. 存在重复元素 II
219. 存在重复元素 II - 力扣(LeetCode) (leetcode-cn.com)class Solution {public: bool containsNearbyDuplicate(vector<int>& nums, int k) { unordered_map<int,int>map; for(int i=0;i<nums.size();i++){ if(map.find(nu原创 2021-09-01 20:27:18 · 42 阅读 · 0 评论 -
268. 丢失的数字
268. 丢失的数字 - 力扣(LeetCode) (leetcode-cn.com)class Solution {public: int missingNumber(vector<int>& nums) { int n=nums.size(); int res; bool flag=false; sort(nums.begin(),nums.end()); for(int i=0;i<原创 2021-09-01 22:45:28 · 44 阅读 · 0 评论 -
414. 第三大的数
414. 第三大的数 - 力扣(LeetCode) (leetcode-cn.com)class Solution {public: int thirdMax(vector<int>& nums) { long long fMax=LLONG_MIN,sMax=LLONG_MIN,tMax=LLONG_MIN; set<long long>set(nums.begin(),nums.end()); for(lon原创 2021-09-04 19:24:45 · 40 阅读 · 0 评论 -
136. 只出现一次的数字
136. 只出现一次的数字 - 力扣(LeetCode) (leetcode-cn.com)class Solution {public: int singleNumber(vector<int>& nums) { int n=nums.size(); sort(nums.begin(),nums.end()); int i=0,j=1; while(i<n&&j<n){原创 2021-08-31 20:30:29 · 39 阅读 · 0 评论 -
349. 两个数组的交集
349. 两个数组的交集 - 力扣(LeetCode) (leetcode-cn.com)1 哈希set: unordered_set<int>res_set2 set和vector之间相互赋值:3 unordered_set<int>nums1_set(nums1.begin(),nums1.end()); nums1是vector类型 vector<int>(res_set.begin(),res_set.end());4 vector 迭代...原创 2021-09-02 21:13:35 · 48 阅读 · 0 评论 -
167. 两数之和 II - 输入有序数组
167. 两数之和 II - 输入有序数组 - 力扣(LeetCode) (leetcode-cn.com)双指针class Solution {public: vector<int> twoSum(vector<int>& numbers, int target) { vector<int>res; int i=0,j=numbers.size()-1; while(i<j){原创 2021-08-31 20:45:59 · 44 阅读 · 0 评论 -
剑指 Offer 10- I. 斐波那契数列
剑指 Offer 10- I. 斐波那契数列 - 力扣(LeetCode) (leetcode-cn.com)class Solution {public: int fib(int n) { if(n==0) return 0; vector<int>res(n+1); res[0]=0; res[1]=1; for(int i=2;i<n+1;i++) res[i]=(res[i-1]+res原创 2021-09-04 22:07:47 · 37 阅读 · 0 评论 -
88. 合并两个有序数组
朴素的想法和朴素的代码:class Solution {public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { vector<int>temp; int i=0,j=0; while(i<m&&j<n){ if(nums1[i]<nums2[j原创 2021-08-31 22:27:13 · 52 阅读 · 0 评论 -
463. 岛屿的周长
463. 岛屿的周长 - 力扣(LeetCode) (leetcode-cn.com)太巧妙了,只关注前一个,如果当前的格子和前一个格子相连,减两条边。class Solution {public: int islandPerimeter(vector<vector<int>>& grid) { int res=0; for(int i=0;i<grid.size();i++){ for(int j原创 2021-09-04 21:13:54 · 29 阅读 · 0 评论 -
169. 多数元素
169. 多数元素 - 力扣(LeetCode) (leetcode-cn.com)class Solution {public: int majorityElement(vector<int>& nums) { int n=nums.size(); if(n==1) return nums[0]; int temp=1,res; sort(nums.begin(),nums.end());原创 2021-08-31 21:23:21 · 42 阅读 · 0 评论 -
204. 计数质数
204. 计数质数 - 力扣(LeetCode) (leetcode-cn.com)找到第一个质数2,去掉所有2的倍数,接着找下一个质数,再去掉所有该质数的倍数class Solution {public: int countPrimes(int n) { int a[n+1]; int count=0; for(int i=2;i<n;i++) a[i]=1; for(int i=2;i<n;i原创 2021-08-31 21:55:24 · 39 阅读 · 0 评论 -
485. 最大连续 1 的个数
485. 最大连续 1 的个数 - 力扣(LeetCode) (leetcode-cn.com)class Solution {public: int findMaxConsecutiveOnes(vector<int>& nums) { int max=0; int temp=0; for(int i=0;i<nums.size();i++){ if(nums[i]==0){原创 2021-09-04 21:21:44 · 51 阅读 · 0 评论 -
217. 存在重复元素
217. 存在重复元素 - 力扣(LeetCode) (leetcode-cn.com)class Solution {public: bool containsDuplicate(vector<int>& nums) { bool flag=false; sort(nums.begin(),nums.end()); for(int i=0;i<nums.size()-1;i++){ if(nu原创 2021-08-31 22:32:55 · 40 阅读 · 0 评论 -
303. 区域和检索 - 数组不可变
303. 区域和检索 - 数组不可变 - 力扣(LeetCode) (leetcode-cn.com)class NumArray { vector<int>nums;public: NumArray(vector<int>& nums) { this->nums=nums; } int sumRange(int left, int right) { int sum=0;原创 2021-09-02 20:27:19 · 45 阅读 · 0 评论 -
买卖股票的最佳时机
121. 买卖股票的最佳时机 - 力扣(LeetCode) (leetcode-cn.com)思想:当前的maxvalue=max{前一天的maxvalue,当前值-当前最小值}class Solution {public: int maxProfit(vector<int>& prices) { if(prices.size()<=1) return 0; int min=prices[0], Max=0; fo原创 2021-08-30 21:34:04 · 41 阅读 · 0 评论 -
448. 找到所有数组中消失的数字
448. 找到所有数组中消失的数字 - 力扣(LeetCode) (leetcode-cn.com)class Solution {public: vector<int> findDisappearedNumbers(vector<int>& nums) { int n=nums.size(); vector<int>res; set<int>set(nums.begin(),nums.e原创 2021-09-04 19:39:40 · 43 阅读 · 0 评论 -
283.移动零
283. 移动零 - 力扣(LeetCode) (leetcode-cn.com)双指针,一个指针盯着非零的数,另一个指针记录。class Solution {public: void moveZeroes(vector<int>& nums) { int i=0,j=0; while(i<nums.size()){ if(nums[i]!=0){ nums[j++]=num原创 2021-09-02 20:22:29 · 73 阅读 · 0 评论 -
455. 分发饼干
455. 分发饼干 - 力扣(LeetCode) (leetcode-cn.com)想当然了,一个升序,一个降序,忽略了[7,8,9,10],[8,7,6,5] 这种情况。class Solution {public: static bool cmp(int a,int b){ return a>b; } int findContentChildren(vector<int>& g, vector<int>&原创 2021-09-04 20:33:00 · 60 阅读 · 0 评论 -
350. 两个数组的交集 II
350. 两个数组的交集 II - 力扣(LeetCode) (leetcode-cn.com)先排序,在比较移动指针class Solution {public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { vector<int>res; sort(nums1.begin(),nums1.end());原创 2021-09-02 21:44:21 · 39 阅读 · 0 评论 -
453. 最小操作次数使数组元素相等
453. 最小操作次数使数组元素相等 - 力扣(LeetCode) (leetcode-cn.com)为何我如此聪明哈哈哈哈哈哈,只要和数学相关class Solution {public: int minMoves(vector<int>& nums) { int count=0; sort(nums.begin(),nums.end()); for(int i=1;i<nums.size();i++){原创 2021-09-04 19:59:18 · 41 阅读 · 0 评论