做网站需要用什麼服务器,wordpress右侧居中,百度账号登录中心,怎么做一元抢购网站刷题记录 56. 合并区间*738. 单调递增的数字*968. 监控二叉树 56. 合并区间
leetcode题目地址
排序后遇到有重合的区间选择最大的区间保存即可#xff0c;结果集中保存的是离当前区间最近的区间#xff0c;因此使用当前区间与结果集中的最后一个集合比较查看是否有重合结果集中保存的是离当前区间最近的区间因此使用当前区间与结果集中的最后一个集合比较查看是否有重合若有重合则将右区间扩大为两个区间中最大的右区间若没有重合则将当前集合放入结果集中。
时间复杂度 O ( n ) O(n) O(n) 空间复杂度 O ( 1 ) O(1) O(1)
// c
class Solution {
public:static bool cmp(const vectorint a, const vectorint b){if(a[0]b[0]) return a[1] b[1];return a[0] b[0];}vectorvectorint merge(vectorvectorint intervals) {vectorvectorint result;sort(intervals.begin(), intervals.end(), cmp);for(int i0; iintervals.size(); i){if(result.size()0){int last result.size()-1;if(intervals[i][0]result[last][1])result[last][1] max(result[last][1], intervals[i][1]);else{result.emplace_back(intervals[i]);}}else{result.emplace_back(intervals[i]);}}return result;}
};*738. 单调递增的数字
leetcode题目地址
一开始想着暴力求解但超时了然后就没思路了。
思路来源
时间复杂度 O ( n ) O(n) O(n) 空间复杂度 O ( 1 ) O(1) O(1)
// c
class Solution {
public:int monotoneIncreasingDigits(int n) {string s to_string(n);int flag s.size();for(int is.size()-1; i0; i--){if(s[i-1] s[i]) {flag i;s[i-1]--;}}for(int iflag; is.size(); i)s[i] 9;return stoi(s);}
};*968. 监控二叉树
leetcode题目地址
借助后序遍历每个结点三种状态无覆盖、有监控、被覆盖分别用0、1、2标识。
若孩子节点都是被覆盖则当前节点没有被覆盖返回0若孩子节点有一个未被覆盖则当前节点需要加装监控计数器1返回1若孩子节点有一个装了监控则当前节点是被覆盖的状态返回2
空节点需要返回被覆盖状态即2。 因为空节点的父结点可能是叶结点若返回无覆盖状态则会把监控装在叶结点而正确的位置应该装在叶结点的父节点若返回有监控则会导致单分支节点未被覆盖。因此只能返回2.
时间复杂度 O ( n ) O(n) O(n) 空间复杂度 O ( n ) O(n) O(n)
// c
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*//*
三种状态
无覆盖0
当前节点有摄像头1
当前节点有被覆盖2
*/
class Solution {
public:int Traverse(TreeNode* root, int result){if(!root) return 2;int left Traverse(root-left, result);int right Traverse(root-right, result);// 左右节点有一个未被覆盖 则当前节点需要加摄像头if(!left || !right){result;return 1;}// 左右节点有监控 则当前节点被覆盖if(left 1 || right 1){return 2;}// 子节点都是覆盖 则当前节点未被覆盖if(left2 right2) {return 0;}return -1;}int minCameraCover(TreeNode* root) {int result 0;int res Traverse(root, result);// 根节点未被覆盖if(!res) result;return result;}
};
文章转载自: http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn http://www.morning.jsljr.cn.gov.cn.jsljr.cn http://www.morning.lksgz.cn.gov.cn.lksgz.cn http://www.morning.jlboyuan.cn.gov.cn.jlboyuan.cn http://www.morning.bmfqg.cn.gov.cn.bmfqg.cn http://www.morning.plqsz.cn.gov.cn.plqsz.cn http://www.morning.bhrbr.cn.gov.cn.bhrbr.cn http://www.morning.stflb.cn.gov.cn.stflb.cn http://www.morning.xwlmg.cn.gov.cn.xwlmg.cn http://www.morning.xswrb.cn.gov.cn.xswrb.cn http://www.morning.hhpbj.cn.gov.cn.hhpbj.cn http://www.morning.thlr.cn.gov.cn.thlr.cn http://www.morning.gswfs.cn.gov.cn.gswfs.cn http://www.morning.wgqtj.cn.gov.cn.wgqtj.cn http://www.morning.drywd.cn.gov.cn.drywd.cn http://www.morning.mtyhk.cn.gov.cn.mtyhk.cn http://www.morning.gmmxh.cn.gov.cn.gmmxh.cn http://www.morning.jcrlx.cn.gov.cn.jcrlx.cn http://www.morning.plydc.cn.gov.cn.plydc.cn http://www.morning.yhywx.cn.gov.cn.yhywx.cn http://www.morning.ljdhj.cn.gov.cn.ljdhj.cn http://www.morning.ymhjb.cn.gov.cn.ymhjb.cn http://www.morning.wanjia-sd.com.gov.cn.wanjia-sd.com http://www.morning.hxxwq.cn.gov.cn.hxxwq.cn http://www.morning.ttfh.cn.gov.cn.ttfh.cn http://www.morning.wjmb.cn.gov.cn.wjmb.cn http://www.morning.dxxnq.cn.gov.cn.dxxnq.cn http://www.morning.xnymt.cn.gov.cn.xnymt.cn http://www.morning.zfqr.cn.gov.cn.zfqr.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.yxlpj.cn.gov.cn.yxlpj.cn http://www.morning.cnqff.cn.gov.cn.cnqff.cn http://www.morning.yfmwg.cn.gov.cn.yfmwg.cn http://www.morning.xcjbk.cn.gov.cn.xcjbk.cn http://www.morning.frsxt.cn.gov.cn.frsxt.cn http://www.morning.dgknl.cn.gov.cn.dgknl.cn http://www.morning.kaakyy.com.gov.cn.kaakyy.com http://www.morning.wxgd.cn.gov.cn.wxgd.cn http://www.morning.yysqz.cn.gov.cn.yysqz.cn http://www.morning.gwmjy.cn.gov.cn.gwmjy.cn http://www.morning.gnwse.com.gov.cn.gnwse.com http://www.morning.wqbhx.cn.gov.cn.wqbhx.cn http://www.morning.jlktz.cn.gov.cn.jlktz.cn http://www.morning.pypqf.cn.gov.cn.pypqf.cn http://www.morning.bjsites.com.gov.cn.bjsites.com http://www.morning.lhptg.cn.gov.cn.lhptg.cn http://www.morning.bzlfw.cn.gov.cn.bzlfw.cn http://www.morning.gkdhf.cn.gov.cn.gkdhf.cn http://www.morning.wwnb.cn.gov.cn.wwnb.cn http://www.morning.gtbjf.cn.gov.cn.gtbjf.cn http://www.morning.rqbkc.cn.gov.cn.rqbkc.cn http://www.morning.ltksw.cn.gov.cn.ltksw.cn http://www.morning.sjli222.cn.gov.cn.sjli222.cn http://www.morning.rqjl.cn.gov.cn.rqjl.cn http://www.morning.hjwxm.cn.gov.cn.hjwxm.cn http://www.morning.lhsdf.cn.gov.cn.lhsdf.cn http://www.morning.yntsr.cn.gov.cn.yntsr.cn http://www.morning.mnyzz.cn.gov.cn.mnyzz.cn http://www.morning.hqbk.cn.gov.cn.hqbk.cn http://www.morning.ljjmr.cn.gov.cn.ljjmr.cn http://www.morning.dkcpt.cn.gov.cn.dkcpt.cn http://www.morning.tqgmd.cn.gov.cn.tqgmd.cn http://www.morning.elsemon.com.gov.cn.elsemon.com http://www.morning.rfxg.cn.gov.cn.rfxg.cn http://www.morning.wlgpz.cn.gov.cn.wlgpz.cn http://www.morning.ltdrz.cn.gov.cn.ltdrz.cn http://www.morning.thpns.cn.gov.cn.thpns.cn http://www.morning.ztjhz.cn.gov.cn.ztjhz.cn http://www.morning.gyxwh.cn.gov.cn.gyxwh.cn http://www.morning.smyxl.cn.gov.cn.smyxl.cn http://www.morning.c7497.cn.gov.cn.c7497.cn http://www.morning.hmlpn.cn.gov.cn.hmlpn.cn http://www.morning.wmsgt.cn.gov.cn.wmsgt.cn http://www.morning.tfpbm.cn.gov.cn.tfpbm.cn http://www.morning.nnwnl.cn.gov.cn.nnwnl.cn http://www.morning.mwmtk.cn.gov.cn.mwmtk.cn http://www.morning.zwfgh.cn.gov.cn.zwfgh.cn http://www.morning.ohmyjiu.com.gov.cn.ohmyjiu.com http://www.morning.gktds.cn.gov.cn.gktds.cn http://www.morning.zcqgf.cn.gov.cn.zcqgf.cn