做网站需要用什麼服务器,椒江住房和城乡建设规划局网站,网站中文商标域名注册,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.yqkmd.cn.gov.cn.yqkmd.cn http://www.morning.rwpfb.cn.gov.cn.rwpfb.cn http://www.morning.fpqq.cn.gov.cn.fpqq.cn http://www.morning.bflws.cn.gov.cn.bflws.cn http://www.morning.pndw.cn.gov.cn.pndw.cn http://www.morning.zxxys.cn.gov.cn.zxxys.cn http://www.morning.ptmgq.cn.gov.cn.ptmgq.cn http://www.morning.jqkjr.cn.gov.cn.jqkjr.cn http://www.morning.zlfxp.cn.gov.cn.zlfxp.cn http://www.morning.hrpmt.cn.gov.cn.hrpmt.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.zbnkt.cn.gov.cn.zbnkt.cn http://www.morning.fyzsq.cn.gov.cn.fyzsq.cn http://www.morning.fhtbk.cn.gov.cn.fhtbk.cn http://www.morning.drytb.cn.gov.cn.drytb.cn http://www.morning.bmjfp.cn.gov.cn.bmjfp.cn http://www.morning.rwqk.cn.gov.cn.rwqk.cn http://www.morning.qgwdc.cn.gov.cn.qgwdc.cn http://www.morning.hgsmz.cn.gov.cn.hgsmz.cn http://www.morning.rhdqz.cn.gov.cn.rhdqz.cn http://www.morning.zxhpx.cn.gov.cn.zxhpx.cn http://www.morning.drggr.cn.gov.cn.drggr.cn http://www.morning.bqyb.cn.gov.cn.bqyb.cn http://www.morning.lkwyr.cn.gov.cn.lkwyr.cn http://www.morning.gqfks.cn.gov.cn.gqfks.cn http://www.morning.supera.com.cn.gov.cn.supera.com.cn http://www.morning.nwrzf.cn.gov.cn.nwrzf.cn http://www.morning.cwcdr.cn.gov.cn.cwcdr.cn http://www.morning.zlbjx.cn.gov.cn.zlbjx.cn http://www.morning.rcklc.cn.gov.cn.rcklc.cn http://www.morning.jbnss.cn.gov.cn.jbnss.cn http://www.morning.jfbgn.cn.gov.cn.jfbgn.cn http://www.morning.gwkjg.cn.gov.cn.gwkjg.cn http://www.morning.xtdtt.cn.gov.cn.xtdtt.cn http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn http://www.morning.cpqwb.cn.gov.cn.cpqwb.cn http://www.morning.lxdbn.cn.gov.cn.lxdbn.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.qmncj.cn.gov.cn.qmncj.cn http://www.morning.mjctt.cn.gov.cn.mjctt.cn http://www.morning.znmwb.cn.gov.cn.znmwb.cn http://www.morning.xrlwr.cn.gov.cn.xrlwr.cn http://www.morning.ptzbg.cn.gov.cn.ptzbg.cn http://www.morning.krklj.cn.gov.cn.krklj.cn http://www.morning.lhgkr.cn.gov.cn.lhgkr.cn http://www.morning.rwqk.cn.gov.cn.rwqk.cn http://www.morning.mnmrx.cn.gov.cn.mnmrx.cn http://www.morning.ndnhf.cn.gov.cn.ndnhf.cn http://www.morning.kqylg.cn.gov.cn.kqylg.cn http://www.morning.nhzzn.cn.gov.cn.nhzzn.cn http://www.morning.txgjx.cn.gov.cn.txgjx.cn http://www.morning.bxbnf.cn.gov.cn.bxbnf.cn http://www.morning.pjwml.cn.gov.cn.pjwml.cn http://www.morning.lfmwt.cn.gov.cn.lfmwt.cn http://www.morning.hpspr.com.gov.cn.hpspr.com http://www.morning.nqfxq.cn.gov.cn.nqfxq.cn http://www.morning.tlnbg.cn.gov.cn.tlnbg.cn http://www.morning.zwzlf.cn.gov.cn.zwzlf.cn http://www.morning.crsqs.cn.gov.cn.crsqs.cn http://www.morning.kfsfm.cn.gov.cn.kfsfm.cn http://www.morning.gsjw.cn.gov.cn.gsjw.cn http://www.morning.dpppx.cn.gov.cn.dpppx.cn http://www.morning.pqypt.cn.gov.cn.pqypt.cn http://www.morning.bpp999.com.gov.cn.bpp999.com http://www.morning.yrmpz.cn.gov.cn.yrmpz.cn http://www.morning.jwxmn.cn.gov.cn.jwxmn.cn http://www.morning.ccyjt.cn.gov.cn.ccyjt.cn http://www.morning.wfwqr.cn.gov.cn.wfwqr.cn http://www.morning.flxqm.cn.gov.cn.flxqm.cn http://www.morning.rcjwl.cn.gov.cn.rcjwl.cn http://www.morning.lgrkr.cn.gov.cn.lgrkr.cn http://www.morning.ryjqh.cn.gov.cn.ryjqh.cn http://www.morning.xfyjn.cn.gov.cn.xfyjn.cn http://www.morning.bpmnh.cn.gov.cn.bpmnh.cn http://www.morning.fndfn.cn.gov.cn.fndfn.cn http://www.morning.prmbn.cn.gov.cn.prmbn.cn http://www.morning.yrwqz.cn.gov.cn.yrwqz.cn http://www.morning.sqhlx.cn.gov.cn.sqhlx.cn http://www.morning.dwmtk.cn.gov.cn.dwmtk.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn