![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
L_Aster
..
展开
-
[leetcode周赛198] 5464. 换酒问题
小区便利店正在促销,用 numExchange 个空酒瓶可以兑换一瓶新酒。你购入了 numBottles 瓶酒。如果喝掉了酒瓶中的酒,那么酒瓶就会变成空的。请你计算 最多 能喝到多少瓶酒。示例 1:输入:numBottles = 9, numExchange = 3输出:13解释:你可以用 3 个空酒瓶兑换 1 瓶酒。所以最多能喝到 9 + 3 + 1 = 13 瓶酒。示例2输入:numBottles = 15, numExchange = 4输出:19解释:你可以用 4 个空酒瓶兑原创 2020-07-19 12:52:19 · 359 阅读 · 0 评论 -
[leetcode周赛198] 5465. 子树中标签相同的节点数
给你一棵树(即,一个连通的无环无向图),这棵树由编号从 0 到 n - 1 的 n 个节点组成,且恰好有 n - 1 条 edges 。树的根节点为节点 0 ,树上的每一个节点都有一个标签,也就是字符串 labels 中的一个小写字符(编号为 i 的 节点的标签就是 labels[i] )边数组 edges 以 edges[i] = [ai, bi] 的形式给出,该格式表示节点 ai 和 bi 之间存在一条边。返回一个大小为 n 的数组,其中 ans[i] 表示第 i 个节点的子树中与节点 i 标签相原创 2020-07-19 12:46:13 · 365 阅读 · 0 评论 -
[leetcode周赛198] 5467. 找到最接近目标值的函数值
func(arr, l, r){ if(r < l){ return -1000000000 } ans = arr[l] for(i = l + 1; i <= r; i++){ ans = ans & arr[i] } return ans}Winston 构造了一个如上所示的函数 func 。他有一个整数数组 arr 和一个整数 target ,他想找到让 |func(arr, l, r) - target| 最小的 l 和 r 。请你返回 |func(原创 2020-07-19 12:35:11 · 366 阅读 · 0 评论 -
200. 岛屿的个数
200. 岛屿的个数class Solution {public: bool isvalid(int x,int y,int m,int n){ return x>=0&&x<m&&y>=0&&y<n; } int numIslands(vector<vector<ch...原创 2018-04-01 16:13:40 · 601 阅读 · 0 评论 -
111. 二叉树的最小深度
111. 二叉树的最小深度class Solution {public: int minDepth(TreeNode* root) { if(!root) return 0; int mindepth=0x7fffffff; mDhelp(root,1,mindepth); return mindepth; }...原创 2018-04-01 15:49:29 · 160 阅读 · 0 评论 -
101. 对称二叉树
101. 对称二叉树class Solution {public: bool isSymmetric(TreeNode* root) { if(root==NULL) return true; return sychelp(root->left,root->right); } bool sychelp(TreeNode ...原创 2018-04-01 15:36:00 · 485 阅读 · 0 评论 -
107. 二叉树的层次遍历 II
107. 二叉树的层次遍历 IIclass Solution {public: vector<vector<int>> levelOrderBottom(TreeNode* root) { vector<vector<int>> res; lOBhelp(res,root,0); reve...原创 2018-04-01 15:17:09 · 1387 阅读 · 0 评论 -
684. 冗余连接
684. 冗余连接 并查集class Solution {public: vector<int> findRedundantConnection(vector<vector<int>>& edges) { int n=edges.size(); vector<int> parents(n+1);...原创 2018-04-01 18:05:36 · 889 阅读 · 0 评论 -
[LeetCode] 64. Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at a...原创 2018-03-20 21:50:57 · 168 阅读 · 0 评论 -
199. 二叉树的右视图
199. 二叉树的右视图class Solution {public: vector<int> rightSideView(TreeNode* root) { vector<int> res; rSVhelp(root,res,0); return res; } void rSVhelp(Tree...原创 2018-04-01 16:39:44 · 563 阅读 · 0 评论 -
515. 在每个树行中找最大值
515. 在每个树行中找最大值class Solution {public: vector<int> largestValues(TreeNode* root) { vector<int> res; lVhelp(root,res,0); return res; } void lVhelp(Tre...原创 2018-04-01 16:45:17 · 418 阅读 · 0 评论 -
207. 课程表
207. 课程表class Solution {public: bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) { vector<int> indegree(numCourses,0); vector<vecto...原创 2018-04-01 17:02:37 · 277 阅读 · 0 评论 -
210. 课程表 II
210. 课程表 IIclass Solution {public: vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) { vector<int> indegree(numCourses,0),res; ...原创 2018-04-01 17:42:56 · 324 阅读 · 0 评论 -
169. 求众数
169. 求众数 虽然用map记录也能过,但是明显其复杂度高,一个o(n)的解法(貌似是某年408算法题)class Solution {public: int majorityElement(vector<int>& nums) { int res=0,counts=0; for(int x:nums){ ...原创 2018-04-02 07:37:06 · 1926 阅读 · 1 评论 -
513. 找树左下角的值
513. 找树左下角的值class Solution {public: int findBottomLeftValue(TreeNode* root) { int maxdep=0,res=0; help(root,1,maxdep,res); return res; } void help(TreeNode *r,in...原创 2018-04-02 07:43:08 · 456 阅读 · 0 评论 -
491. 递增子序列
491. 递增子序列class Solution {public: vector<vector<int>> findSubsequences(vector<int>& nums) { set<vector<int>> res; vector<int> rp; ...原创 2018-04-02 08:29:04 · 417 阅读 · 0 评论 -
310. Minimum Height Trees
310. Minimum Height Trees 从叶节点开始,依次删除他们,最后剩下1或者2个节点的就是树根,有点类似拓扑排序的感觉 参考:https://leetcode.com/problems/minimum-height-trees/discuss/76055/Share-some-thoughtsclass Solution {public: vector<i...原创 2018-04-03 10:03:14 · 125 阅读 · 0 评论 -
1. 两数之和
1. 两数之和class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int,int> mp; for(size_t i=0;i<nums.size();++i){ ...原创 2018-04-05 08:01:23 · 134 阅读 · 0 评论 -
[LeetCode] 84. Largest Rectangle in Histogram
Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width of each...原创 2018-03-24 13:41:16 · 166 阅读 · 0 评论 -
[LeetCode] 746. Min Cost Climbing Stairs
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of ...原创 2018-03-04 11:18:07 · 163 阅读 · 0 评论 -
[LeetCode] 22. Generate Parentheses
void pHelp(vector> &res,vector &rp,vector &nums,unordered_map &vis){ if(rp.size()==nums.size()) { res.push_back(rp); return; } for(unsigned int i=0;i<nums.size();++i) { if(vis[num原创 2017-09-03 13:08:46 · 205 阅读 · 0 评论 -
[LeetCode] 547. Friend Circles
int findCircleNum(vector<vector<int>>& M){ int n=M.size(); vector<int> parents(n,0); for(int i=0;i<n;++i) { parents[i]=i; } function<int(int,vector<int>&)>finds=[&finds原创 2017-09-05 13:45:47 · 314 阅读 · 0 评论 -
[LeetCode] 4.Median of Two Sorted Arrays
原题链接:Median of Two Sorted Arrays 题目大意是,给你两个已经排好序的数组,找到这两个数组的中位数 比如:Example 1:nums1 = [1, 3]nums2 = [2]The median is 2.0又比如:Example 2:nums1 = [1, 2]nums2 = [3, 4]The median is (2 + 3)/2 = 2.5问题可以延原创 2017-08-17 13:36:05 · 215 阅读 · 0 评论 -
[LeetCode] 560. Subarray Sum Equals K
int subarraySum2(vector& nums, int k)//use hash{ int sum=0,cnt=0; unordered_map hash; hash[0]=1; for(int x:nums) { sum+=x; cnt+=hash[sum-k]; ++hash[sum]; } return cnt;}原创 2017-08-30 13:48:51 · 226 阅读 · 0 评论 -
[LeetCode] 39. Combination Sum
回溯void cbhelp(vector> &res,vector &v,int target,int i,vector recp){ if(target<0) return; else if(target==0) { res.push_back(recp); return; } for(unsigned int k=i;k<v.size();++k) { recp.原创 2017-08-31 12:03:23 · 212 阅读 · 0 评论 -
[LeetCode] 40. Combination Sum II
注意和39题区别,当前层值相同则跳过回溯法void cb2help(vector> &res,vector &v,int target,unsigned int i,vector recp){ if(target<0) return; else if(target==0) { res.push_back(recp); return; } for(unsigned int原创 2017-08-31 22:39:05 · 178 阅读 · 0 评论 -
[LeetCode] 650. 2 Keys Keyboard
题目链接:点击打开链接dpint minSteps(int n){ vector dp(n+1,1024); dp[1]=0; for(int i=2;i<=n;++i) { for(int k=1;k*k<=i;++k) { if(i%k==0) dp[i]=min(min(dp[k]+i/k,dp[i/k]+k),dp[i]); } } retu原创 2017-08-31 13:43:29 · 267 阅读 · 0 评论 -
[LeetCode] 442. Find All Duplicates in an Array
题解来自点击打开链接,vector findDuplicates(vector& nums){ vector res; for(unsigned int i=0;i<nums.size();++i) { nums[abs(nums[i])-1]=-nums[abs(nums[i])-1]; if(nums[abs(nums[i])-1]>0) res.push_back(abs转载 2017-08-31 23:05:37 · 172 阅读 · 0 评论 -
[LeetCode] 1. Two Sum
vector twoSum(vector& nums, int target){ unordered_map hash; int n=nums.size(); for(int i=0;i<n;++i) { if(hash[target-nums[i]]) return {i,hash[target-nums[i]]-1}; hash[nums[i]]=i+1; } retur原创 2017-09-01 00:12:04 · 178 阅读 · 0 评论 -
[LeetCode] 143. Reorder List
void reorderList(ListNode* head){ if(head==NULL||head->next==NULL) return; ListNode *l1=head,*l2=head->next; while(l2&&l2->next) { l1=l1->next; l2=l2->next->next; } ListNode *h=l1-原创 2017-09-11 11:33:01 · 221 阅读 · 0 评论 -
[LeetCode] 637. Average of Levels in Binary Tree
void aveLevHelp(vector &res,vector &cnt,unsigned int lev,TreeNode *r){ if(r==NULL) return; if(lev+1>res.size()) { res.push_back(0); cnt.push_back(0); } res[lev]+=r->val; cnt[lev]++原创 2017-09-02 15:32:09 · 436 阅读 · 0 评论 -
[LeetCode] 543. Diameter of Binary Tree
int dbHelp(TreeNode *r,int &res){ if(!r)return 0; int lh=dbHelp(r->left,res)+1; int rh=dbHelp(r->right,res)+1; res=max(res,lh+rh-2); return max(lh,rh);}int diameterOfBinaryTree(TreeNod原创 2017-09-02 16:54:17 · 205 阅读 · 0 评论 -
[LeetCode] 107. Binary Tree Level Order Traversal II
void lobHelp(vector> &res,unsigned int lev,TreeNode *r){ if(r==NULL) return; if(lev+1>res.size()) { res.push_back(vector()); } res[lev].push_back(r->val); lobHelp(res,lev+1,r->left);原创 2017-09-02 17:51:04 · 321 阅读 · 0 评论 -
[LeetCode] 662. Maximum Width of Binary Tree
void wbtHelp(vector &lf,int idx,unsigned int lev,int &maxwid,TreeNode *r){ if(r==NULL) return; if(lev+1>lf.size()) lf.push_back(idx); else maxwid=max(maxwid,idx+1-lf[lev]); wbtHelp(lf,2*idx,原创 2017-09-02 18:29:27 · 599 阅读 · 0 评论 -
[LeetCode] 103. Binary Tree Zigzag Level Order Traversal
void zzloHelp(vector> &res,unsigned int lev,TreeNode *r){ if(!r)return; if(lev+1>res.size()) { res.push_back(vector()); } res[lev].push_back(r->val); zzloHelp(res,lev+1,r->left); zz原创 2017-09-02 18:42:24 · 194 阅读 · 0 评论 -
[LeetCode] 78. Subsets
void ssHelp(vector> &res,vector &nums,vector &rp,unsigned int cur){ res.push_back(rp); for(unsigned int i=cur;i<nums.size();++i) { rp.push_back(nums[i]); ssHelp(res,nums,rp,i+1); rp.p原创 2017-09-03 11:49:21 · 232 阅读 · 0 评论 -
[LeetCode] 526. Beautiful Arrangement
void caHelp(int &res,vector &v,int N,int cnt){ if(cnt==N+1) { ++res; return; } for(int i=1;i<=N;++i) { if(v[i]==0&&(i%cnt==0||cnt%i==0)) { v[i]=1; caHelp(res,v,N,cnt+1)原创 2017-09-03 12:24:38 · 239 阅读 · 0 评论