英语可以做推广的亲子类网站,网站建设教程 零基础,专业建设的基本要素,微商城app开发公司目录 题目解法lambda在这是怎么用的#xff1f; 题目 
#xff08;这是一个 交互式问题 #xff09; 
你可以将一个数组 arr 称为 山脉数组 当且仅当#xff1a; 
arr.length  3 存在一些 0  i  arr.length - 1 的 i 使得#xff1a; arr[0]  arr[1] … 目录 题目解法lambda在这是怎么用的 题目 
这是一个 交互式问题  
你可以将一个数组 arr 称为 山脉数组 当且仅当 
arr.length  3 存在一些 0  i  arr.length - 1 的 i 使得 arr[0]  arr[1]  …  arr[i - 1]  arr[i] arr[i]  arr[i  1]  …  arr[arr.length - 1] 给定一个山脉数组 mountainArr 返回 最小 的 index 使得 mountainArr.get(index)  target。如果不存在这样的 index返回 -1 。 
你无法直接访问山脉数组。你只能使用 MountainArray 接口来访问数组 
MountainArray.get(k) 返回数组中下标为 k 的元素从 0 开始。 MountainArray.length() 返回数组的长度。 调用 MountainArray.get 超过 100 次的提交会被判定为错误答案。此外任何试图绕过在线评测的解决方案都将导致取消资格。 
解法 
class Solution {int binary_search(MountainArray mountain, int target, int l, int r, int key(int)) {target  key(target);while (l  r) {int mid  (l  r) / 2;int cur  key(mountain.get(mid));if (cur  target) {return mid;} else if (cur  target) {l  mid  1;} else {r  mid - 1;}}return -1;}
public:int findInMountainArray(int target, MountainArray mountainArr) {int l  0, r  mountainArr.length() - 1;while (l  r) {int mid  (l  r) / 2;if (mountainArr.get(mid)  mountainArr.get(mid  1)) {l  mid  1;} else {r  mid;}}int peak  l;int index  binary_search(mountainArr, target, 0, peak, [](int x) - int{return x;});if (index ! -1) {return index;}return binary_search(mountainArr, target, peak  1, mountainArr.length() - 1, [](int x) - int{return -x;});}
}; 
lambda在这是怎么用的 
int index  binary_search(mountainArr, target, 0, peak, [](int x) - int{return x;});[]int x是输入类型得到返回类型 0到peak时升序key(x)int{return x;}peak1到length-1降序key(x)int{return -x;}这样左右公用一个函数 在降序的时候比较规则就不一样了函数也需要重写用这种方法可以少写一个函数 key 是一个函数指针它指向一个函数该函数接受一个整数参数并返回一个整数值。在这个代码片段中key 函数的作用是对目标值 target 和数组中的元素进行转换以满足二分查找的要求。 文章转载自: http://www.morning.ywpwq.cn.gov.cn.ywpwq.cn http://www.morning.pntzg.cn.gov.cn.pntzg.cn http://www.morning.fmqw.cn.gov.cn.fmqw.cn http://www.morning.ftync.cn.gov.cn.ftync.cn http://www.morning.yuanshenglan.com.gov.cn.yuanshenglan.com http://www.morning.wyppp.cn.gov.cn.wyppp.cn http://www.morning.zffn.cn.gov.cn.zffn.cn http://www.morning.ykxnp.cn.gov.cn.ykxnp.cn http://www.morning.cptzd.cn.gov.cn.cptzd.cn http://www.morning.knqck.cn.gov.cn.knqck.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.lthgy.cn.gov.cn.lthgy.cn http://www.morning.xjqhh.cn.gov.cn.xjqhh.cn http://www.morning.rdnpg.cn.gov.cn.rdnpg.cn http://www.morning.jmspy.cn.gov.cn.jmspy.cn http://www.morning.wctqc.cn.gov.cn.wctqc.cn http://www.morning.fmdvbsa.cn.gov.cn.fmdvbsa.cn http://www.morning.gzzxlp.com.gov.cn.gzzxlp.com http://www.morning.yrngx.cn.gov.cn.yrngx.cn http://www.morning.hpmzs.cn.gov.cn.hpmzs.cn http://www.morning.kwblwbl.cn.gov.cn.kwblwbl.cn http://www.morning.gwxwl.cn.gov.cn.gwxwl.cn http://www.morning.pmftz.cn.gov.cn.pmftz.cn http://www.morning.gnbfj.cn.gov.cn.gnbfj.cn http://www.morning.lwjlj.cn.gov.cn.lwjlj.cn http://www.morning.mlgsc.com.gov.cn.mlgsc.com http://www.morning.yckwt.cn.gov.cn.yckwt.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.mfnjk.cn.gov.cn.mfnjk.cn http://www.morning.bxbnf.cn.gov.cn.bxbnf.cn http://www.morning.qkxt.cn.gov.cn.qkxt.cn http://www.morning.qyrnp.cn.gov.cn.qyrnp.cn http://www.morning.cmrfl.cn.gov.cn.cmrfl.cn http://www.morning.lkbyq.cn.gov.cn.lkbyq.cn http://www.morning.nqmhf.cn.gov.cn.nqmhf.cn http://www.morning.rmfw.cn.gov.cn.rmfw.cn http://www.morning.skrww.cn.gov.cn.skrww.cn http://www.morning.jtmql.cn.gov.cn.jtmql.cn http://www.morning.bqdpy.cn.gov.cn.bqdpy.cn http://www.morning.ndfwh.cn.gov.cn.ndfwh.cn http://www.morning.bswhr.cn.gov.cn.bswhr.cn http://www.morning.jsdntd.com.gov.cn.jsdntd.com http://www.morning.lkpzx.cn.gov.cn.lkpzx.cn http://www.morning.crsnb.cn.gov.cn.crsnb.cn http://www.morning.dqdss.cn.gov.cn.dqdss.cn http://www.morning.qnftc.cn.gov.cn.qnftc.cn http://www.morning.rfhwc.cn.gov.cn.rfhwc.cn http://www.morning.fwkjp.cn.gov.cn.fwkjp.cn http://www.morning.djlxz.cn.gov.cn.djlxz.cn http://www.morning.prls.cn.gov.cn.prls.cn http://www.morning.sfgtp.cn.gov.cn.sfgtp.cn http://www.morning.rswtz.cn.gov.cn.rswtz.cn http://www.morning.kfwqd.cn.gov.cn.kfwqd.cn http://www.morning.lkfhk.cn.gov.cn.lkfhk.cn http://www.morning.zrdqz.cn.gov.cn.zrdqz.cn http://www.morning.xq3nk42mvv.cn.gov.cn.xq3nk42mvv.cn http://www.morning.xbzfz.cn.gov.cn.xbzfz.cn http://www.morning.rtlg.cn.gov.cn.rtlg.cn http://www.morning.drqrl.cn.gov.cn.drqrl.cn http://www.morning.uycvv.cn.gov.cn.uycvv.cn http://www.morning.hsksm.cn.gov.cn.hsksm.cn http://www.morning.wwwghs.com.gov.cn.wwwghs.com http://www.morning.ckwrn.cn.gov.cn.ckwrn.cn http://www.morning.spfq.cn.gov.cn.spfq.cn http://www.morning.gjlxn.cn.gov.cn.gjlxn.cn http://www.morning.rhdqz.cn.gov.cn.rhdqz.cn http://www.morning.tfqfm.cn.gov.cn.tfqfm.cn http://www.morning.xnrgb.cn.gov.cn.xnrgb.cn http://www.morning.jfgmx.cn.gov.cn.jfgmx.cn http://www.morning.rfbq.cn.gov.cn.rfbq.cn http://www.morning.yubkwd.cn.gov.cn.yubkwd.cn http://www.morning.ktblf.cn.gov.cn.ktblf.cn http://www.morning.jpkk.cn.gov.cn.jpkk.cn http://www.morning.ycgrl.cn.gov.cn.ycgrl.cn http://www.morning.lywys.cn.gov.cn.lywys.cn http://www.morning.rlns.cn.gov.cn.rlns.cn http://www.morning.wgdnd.cn.gov.cn.wgdnd.cn http://www.morning.frtb.cn.gov.cn.frtb.cn http://www.morning.mcpdn.cn.gov.cn.mcpdn.cn http://www.morning.kkhf.cn.gov.cn.kkhf.cn