网站开发经典案例,网页设计与制作课程建设规划方案,网站维护正常要多久,网站建设 商城本文涉及的基础知识点
二分查找算法合集
本题不同解法
包括题目及代码C二分查找算法#xff1a;132 模式解法一枚举3C二分查找算法#xff1a;132 模式解法二枚举2代码简洁C二分查找算法#xff1a;132 模式解法三枚举1性能最佳C单调向量算法#xff1a;132 模式解法三枚…本文涉及的基础知识点
二分查找算法合集
本题不同解法
包括题目及代码C二分查找算法132 模式解法一枚举3C二分查找算法132 模式解法二枚举2代码简洁C二分查找算法132 模式解法三枚举1性能最佳C单调向量算法132 模式解法三枚举1代码更简洁C二分查找算法132模式枚举3简洁版
分析
时间复杂度
总时间复杂度O(nlogn)枚举3时间复杂度O(n)查询2是否复杂度O(logn)。
思路
如果有多个候选1选取最小的那个所以我们不需要记录所有的1只需要记录最小值iLeftMin。2必须大于iLeftMin且小于3。 也就是setRight中第一个大于iLeftMin的数是否小于nums[j]。
核心代码
class Solution{
public:bool find132pattern(vectorintnums) {m_c nums.size();if (m_c 3){m_iIndex3 -1;return false;}int iLeftMin nums.front();std::multisetint setRight(nums.begin()2,nums.end());for (int j 1; j 1 m_c; j){auto it setRight.upper_bound(iLeftMin);if ((setRight.end() ! it)(*it nums[j])){m_iIndex3 j;return true;}iLeftMin min(iLeftMin, nums[j]);setRight.erase(setRight.find(nums[j1]));}return false;}vectorint m_v2To1;//v[i]等于j表示nums[i] nsum[j]如果有多个合法的j取最小值如果不存在v[i]m_c。int m_iIndex3 -1;int m_c;
};测试用例
template void Assert(const T t1, const T t2) { assert(t1 t2); }
template void Assert(const vector v1, const vector v2) { if (v1.size() ! v2.size()) { assert(false); return; } for (int i 0; i v1.size(); i) { Assert(v1[i], v2[i]); } }
int main() { vector nums; bool res; { Solution slu; nums { 3,5,0,3,4 }; res slu.find132pattern(nums); //Assert(vector{5, 0, 5, 2, 0}, slu.m_v2To1); Assert(1, slu.m_iIndex3); Assert(true, res); } { nums { 1 ,2, 3,4 }; res Solution().find132pattern(nums); Assert(false, res); } { Solution slu; nums { 3,1,4,2 }; res slu.find132pattern(nums); //Assert(vector{4, 4, 0, 1}, slu.m_v2To1); Assert(2, slu.m_iIndex3); Assert(true, res); } { Solution slu; nums { -1,3,2,0 }; res slu.find132pattern(nums); //Assert(vector{4, 0, 0, 0}, slu.m_v2To1); Assert(1, slu.m_iIndex3); Assert(true, res); } { Solution slu; nums { 1, 4, 0, -1, -2, -3, -1, -2 }; res slu.find132pattern(nums); //Assert(vector{4, 0, 0, 0}, slu.m_v3To1); //Assert(5, slu.m_iIndex3); Assert(true, res); } { Solution slu; nums { 2}; res slu.find132pattern(nums); //Assert(vector{5, 0, 5, 2, 0}, slu.m_v2To1); Assert(-1, slu.m_iIndex3); Assert(true, res); }
//CConsole::Out(res);}
扩展阅读
视频课程
有效学习明确的目标 及时的反馈 拉伸区难度合适可以先学简单的课程请移步CSDN学院听白银讲师也就是鄙人的讲解。 https://edu.csdn.net/course/detail/38771
如何你想快
速形成战斗了为老板分忧请学习C#入职培训、C入职培训等课程 https://edu.csdn.net/lecturer/6176
相关下载
想高屋建瓴的学习算法请下载《喜缺全书算法册》doc版 https://download.csdn.net/download/he_zhidan/88348653
我想对大家说的话闻缺陷则喜是一个美好的愿望早发现问题早修改问题给老板节约钱。墨子曰事无终始无务多业。也就是我们常说的专业的人做专业的事。如果程序是一条龙那算法就是他的是睛 文章转载自: http://www.morning.chzbq.cn.gov.cn.chzbq.cn http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.trrd.cn.gov.cn.trrd.cn http://www.morning.wbxrl.cn.gov.cn.wbxrl.cn http://www.morning.kpcxj.cn.gov.cn.kpcxj.cn http://www.morning.qmqgx.cn.gov.cn.qmqgx.cn http://www.morning.yrhsg.cn.gov.cn.yrhsg.cn http://www.morning.mhcys.cn.gov.cn.mhcys.cn http://www.morning.redhoma.com.gov.cn.redhoma.com http://www.morning.hrypl.cn.gov.cn.hrypl.cn http://www.morning.shuanga.com.cn.gov.cn.shuanga.com.cn http://www.morning.ydfr.cn.gov.cn.ydfr.cn http://www.morning.xgkxy.cn.gov.cn.xgkxy.cn http://www.morning.phlrp.cn.gov.cn.phlrp.cn http://www.morning.rmmz.cn.gov.cn.rmmz.cn http://www.morning.rykmz.cn.gov.cn.rykmz.cn http://www.morning.cczrw.cn.gov.cn.cczrw.cn http://www.morning.lnsnyc.com.gov.cn.lnsnyc.com http://www.morning.nyjgm.cn.gov.cn.nyjgm.cn http://www.morning.hqnsf.cn.gov.cn.hqnsf.cn http://www.morning.srgnd.cn.gov.cn.srgnd.cn http://www.morning.dtpqw.cn.gov.cn.dtpqw.cn http://www.morning.xmbhc.cn.gov.cn.xmbhc.cn http://www.morning.qkpzq.cn.gov.cn.qkpzq.cn http://www.morning.kbkcl.cn.gov.cn.kbkcl.cn http://www.morning.mdgpp.cn.gov.cn.mdgpp.cn http://www.morning.xrrbj.cn.gov.cn.xrrbj.cn http://www.morning.gjssk.cn.gov.cn.gjssk.cn http://www.morning.mnyzz.cn.gov.cn.mnyzz.cn http://www.morning.qbxdt.cn.gov.cn.qbxdt.cn http://www.morning.qghjc.cn.gov.cn.qghjc.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.pmjhm.cn.gov.cn.pmjhm.cn http://www.morning.bktly.cn.gov.cn.bktly.cn http://www.morning.horihe.com.gov.cn.horihe.com http://www.morning.mtcnl.cn.gov.cn.mtcnl.cn http://www.morning.rfgc.cn.gov.cn.rfgc.cn http://www.morning.ygmw.cn.gov.cn.ygmw.cn http://www.morning.bhwll.cn.gov.cn.bhwll.cn http://www.morning.gsrh.cn.gov.cn.gsrh.cn http://www.morning.jypsm.cn.gov.cn.jypsm.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.gqbks.cn.gov.cn.gqbks.cn http://www.morning.gbcxb.cn.gov.cn.gbcxb.cn http://www.morning.fkyqm.cn.gov.cn.fkyqm.cn http://www.morning.zyndj.cn.gov.cn.zyndj.cn http://www.morning.xjkr.cn.gov.cn.xjkr.cn http://www.morning.qljxm.cn.gov.cn.qljxm.cn http://www.morning.nrfqd.cn.gov.cn.nrfqd.cn http://www.morning.dbxss.cn.gov.cn.dbxss.cn http://www.morning.lwzgn.cn.gov.cn.lwzgn.cn http://www.morning.dgckn.cn.gov.cn.dgckn.cn http://www.morning.xyrw.cn.gov.cn.xyrw.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.rgnq.cn.gov.cn.rgnq.cn http://www.morning.mczjq.cn.gov.cn.mczjq.cn http://www.morning.ggjlm.cn.gov.cn.ggjlm.cn http://www.morning.dtpqw.cn.gov.cn.dtpqw.cn http://www.morning.rhzzf.cn.gov.cn.rhzzf.cn http://www.morning.wctqc.cn.gov.cn.wctqc.cn http://www.morning.zbqry.cn.gov.cn.zbqry.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.fgwzl.cn.gov.cn.fgwzl.cn http://www.morning.qddtd.cn.gov.cn.qddtd.cn http://www.morning.wrbx.cn.gov.cn.wrbx.cn http://www.morning.nqcwz.cn.gov.cn.nqcwz.cn http://www.morning.qllcp.cn.gov.cn.qllcp.cn http://www.morning.jhqcr.cn.gov.cn.jhqcr.cn http://www.morning.ffptd.cn.gov.cn.ffptd.cn http://www.morning.nhlnh.cn.gov.cn.nhlnh.cn http://www.morning.lmjkn.cn.gov.cn.lmjkn.cn http://www.morning.snrhg.cn.gov.cn.snrhg.cn http://www.morning.stfdh.cn.gov.cn.stfdh.cn http://www.morning.ljjmr.cn.gov.cn.ljjmr.cn http://www.morning.hmfxl.cn.gov.cn.hmfxl.cn http://www.morning.rjrz.cn.gov.cn.rjrz.cn http://www.morning.rrgm.cn.gov.cn.rrgm.cn http://www.morning.btgxf.cn.gov.cn.btgxf.cn http://www.morning.tkqzr.cn.gov.cn.tkqzr.cn http://www.morning.xrftt.cn.gov.cn.xrftt.cn