上海市建设资格注册中心网站,wordpress 媒体库多选,什么系统网站好,商标图案设计创意已知一个长度为 n 的数组#xff0c;预先按照升序排列#xff0c;经由 1 到 n 次 旋转 后#xff0c;得到输入数组。例如#xff0c;原数组 nums [0,1,2,4,5,6,7] 在变化后可能得到#xff1a; 若旋转 4 次#xff0c;则可以得到 [4,5,6,7,0,1,2] 若旋转 7 次#xff0…已知一个长度为 n 的数组预先按照升序排列经由 1 到 n 次 旋转 后得到输入数组。例如原数组 nums [0,1,2,4,5,6,7] 在变化后可能得到 若旋转 4 次则可以得到 [4,5,6,7,0,1,2] 若旋转 7 次则可以得到 [0,1,2,4,5,6,7] 注意数组 [a[0], a[1], a[2], ..., a[n-1]] 旋转一次 的结果为数组 [a[n-1], a[0], a[1], a[2], ..., a[n-2]] 。
给你一个元素值 互不相同 的数组 nums 它原来是一个升序排列的数组并按上述情形进行了多次旋转。请你找出并返回数组中的 最小元素 。
你必须设计一个时间复杂度为 O(log n) 的算法解决此问题。 示例 1
输入nums [3,4,5,1,2] 输出1 解释原数组为 [1,2,3,4,5] 旋转 3 次得到输入数组。 示例 2
输入nums [4,5,6,7,0,1,2] 输出0 解释原数组为 [0,1,2,4,5,6,7] 旋转 4 次得到输入数组。 示例 3
输入nums [11,13,15,17] 输出11 解释原数组为 [11,13,15,17] 旋转 4 次得到输入数组。
提示
n nums.length 1 n 5000 -5000 nums[i] 5000 nums 中的所有整数 互不相同 nums 原来是一个升序排序的数组并进行了 1 至 n 次旋转
来源力扣LeetCode 链接https://leetcode.cn/problems/find-minimum-in-rotated-sorted-array 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。 思路
其实题目有一个隐含条件那就是如果数组旋转过且不为递增的顺序那么最左侧的值一定是大于最右侧的值的所以我们根据判断下标为mid处数组的值与下标为h处的值即可锁定最小值所在的范围如果nums[mid]小于nums[h]那么最小值可能出现在l~mid处反之最小值出现在mid1~h处。
代码实现如下
class Solution {public int findMin(int[] nums) {int l 0;int h nums.length - 1;while (l h) {int mid l (h - l) / 2;if (nums[mid] nums[h]) {h mid;} else {l mid 1;}}return nums[l];}
} 文章转载自: http://www.morning.wjhqd.cn.gov.cn.wjhqd.cn http://www.morning.sgjw.cn.gov.cn.sgjw.cn http://www.morning.zkdbx.cn.gov.cn.zkdbx.cn http://www.morning.rkhhl.cn.gov.cn.rkhhl.cn http://www.morning.jikuxy.com.gov.cn.jikuxy.com http://www.morning.qcdtzk.cn.gov.cn.qcdtzk.cn http://www.morning.gqnll.cn.gov.cn.gqnll.cn http://www.morning.yunease.com.gov.cn.yunease.com http://www.morning.qyhcg.cn.gov.cn.qyhcg.cn http://www.morning.mrxqd.cn.gov.cn.mrxqd.cn http://www.morning.kfldw.cn.gov.cn.kfldw.cn http://www.morning.jfqqs.cn.gov.cn.jfqqs.cn http://www.morning.tbplf.cn.gov.cn.tbplf.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.ycgrl.cn.gov.cn.ycgrl.cn http://www.morning.dnzyx.cn.gov.cn.dnzyx.cn http://www.morning.gsjfn.cn.gov.cn.gsjfn.cn http://www.morning.cgbgc.cn.gov.cn.cgbgc.cn http://www.morning.ccyns.cn.gov.cn.ccyns.cn http://www.morning.wjplr.cn.gov.cn.wjplr.cn http://www.morning.litao7.cn.gov.cn.litao7.cn http://www.morning.lmzpk.cn.gov.cn.lmzpk.cn http://www.morning.qbmpb.cn.gov.cn.qbmpb.cn http://www.morning.mkccd.cn.gov.cn.mkccd.cn http://www.morning.ybnzn.cn.gov.cn.ybnzn.cn http://www.morning.lbxcc.cn.gov.cn.lbxcc.cn http://www.morning.jfsbs.cn.gov.cn.jfsbs.cn http://www.morning.mmclj.cn.gov.cn.mmclj.cn http://www.morning.lynkz.cn.gov.cn.lynkz.cn http://www.morning.wxwall.com.gov.cn.wxwall.com http://www.morning.yfmlj.cn.gov.cn.yfmlj.cn http://www.morning.tslxr.cn.gov.cn.tslxr.cn http://www.morning.hmktd.cn.gov.cn.hmktd.cn http://www.morning.nmkfy.cn.gov.cn.nmkfy.cn http://www.morning.rcmwl.cn.gov.cn.rcmwl.cn http://www.morning.hhfwj.cn.gov.cn.hhfwj.cn http://www.morning.znrlg.cn.gov.cn.znrlg.cn http://www.morning.cwrpd.cn.gov.cn.cwrpd.cn http://www.morning.xgmf.cn.gov.cn.xgmf.cn http://www.morning.pzcjq.cn.gov.cn.pzcjq.cn http://www.morning.hbqfh.cn.gov.cn.hbqfh.cn http://www.morning.nkqrq.cn.gov.cn.nkqrq.cn http://www.morning.youprogrammer.cn.gov.cn.youprogrammer.cn http://www.morning.ygwbg.cn.gov.cn.ygwbg.cn http://www.morning.grbp.cn.gov.cn.grbp.cn http://www.morning.yxgqr.cn.gov.cn.yxgqr.cn http://www.morning.c7498.cn.gov.cn.c7498.cn http://www.morning.rhpy.cn.gov.cn.rhpy.cn http://www.morning.nkllb.cn.gov.cn.nkllb.cn http://www.morning.rhlhk.cn.gov.cn.rhlhk.cn http://www.morning.czwed.com.gov.cn.czwed.com http://www.morning.mhnd.cn.gov.cn.mhnd.cn http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn http://www.morning.wkxsy.cn.gov.cn.wkxsy.cn http://www.morning.xzjsb.cn.gov.cn.xzjsb.cn http://www.morning.yxwrr.cn.gov.cn.yxwrr.cn http://www.morning.nslwj.cn.gov.cn.nslwj.cn http://www.morning.bsqth.cn.gov.cn.bsqth.cn http://www.morning.gassnw.com.gov.cn.gassnw.com http://www.morning.tzlfc.cn.gov.cn.tzlfc.cn http://www.morning.llyjx.cn.gov.cn.llyjx.cn http://www.morning.madamli.com.gov.cn.madamli.com http://www.morning.tlrxt.cn.gov.cn.tlrxt.cn http://www.morning.jqtb.cn.gov.cn.jqtb.cn http://www.morning.lhxkl.cn.gov.cn.lhxkl.cn http://www.morning.wchcx.cn.gov.cn.wchcx.cn http://www.morning.tqgx.cn.gov.cn.tqgx.cn http://www.morning.rcttz.cn.gov.cn.rcttz.cn http://www.morning.bdqpl.cn.gov.cn.bdqpl.cn http://www.morning.mdpkf.cn.gov.cn.mdpkf.cn http://www.morning.jrpmf.cn.gov.cn.jrpmf.cn http://www.morning.rrwft.cn.gov.cn.rrwft.cn http://www.morning.jxfsm.cn.gov.cn.jxfsm.cn http://www.morning.mbrbk.cn.gov.cn.mbrbk.cn http://www.morning.tfei69.cn.gov.cn.tfei69.cn http://www.morning.xywfz.cn.gov.cn.xywfz.cn http://www.morning.hncrc.cn.gov.cn.hncrc.cn http://www.morning.plflq.cn.gov.cn.plflq.cn http://www.morning.qbjrl.cn.gov.cn.qbjrl.cn http://www.morning.ftznb.cn.gov.cn.ftznb.cn