博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode-153-Find Minimum in Rotated Sorted Array
阅读量:6852 次
发布时间:2019-06-26

本文共 763 字,大约阅读时间需要 2 分钟。

算法描述:

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e.,  [0,1,2,4,5,6,7] might become  [4,5,6,7,0,1,2]).

Find the minimum element.

You may assume no duplicate exists in the array.

Example 1:

Input: [3,4,5,1,2] Output: 1

Example 2:

Input: [4,5,6,7,0,1,2]Output: 0

解题思路:二分法,注意细节。

if(nums.size()==1) return nums[0];        int left = 0;        int right = nums.size()-1;        while(left < right){            if(nums[left] < nums[right]) return nums[left];            int mid = left + (right - left) / 2;            if(nums[mid] > nums[right]){                left = mid +1;            } else                right = mid;        }        return nums[left];    }

 

转载于:https://www.cnblogs.com/nobodywang/p/10354523.html

你可能感兴趣的文章
hadoop之 HDFS fs 命令总结
查看>>
快速实现DEDECMS织梦系统伪标题采集
查看>>
去除Html标签
查看>>
目的节点序列距离矢量(DSDV)协议
查看>>
iOS之核心动画
查看>>
Linux问题集
查看>>
parent
查看>>
JNI Hello World
查看>>
LindDotNetCore~Ocelot实现微服务网关
查看>>
数据结构之队列——回文字判断
查看>>
消息队列
查看>>
链表的遍历
查看>>
微信小程序中的图形验证码
查看>>
数字转罗马数字
查看>>
CNN网络架构演进
查看>>
windows上安装Ipython notebook
查看>>
选择屏幕加功能码
查看>>
UIImagePickerController 视频录制操作,视频大小,时间长度
查看>>
Python Tuples
查看>>
Entity Framework 4 in Action读书笔记——第一章:数据访问重载:Entity Framework(3)...
查看>>