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

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

Suppose a sorted array 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.

 Notice

You may assume no duplicate exists in the array.

Example

Given [4, 5, 6, 7, 0, 1, 2] return 0

 

OOXX二分法(find the first X)。特征:相对最后一个数的大小。O:>nums[end]; X: <=nums[end]。

切记要和最后一个数比,不可以和第一个数比,因为和第一个数比在没有rotate过的特殊情况里会找空。

 

public class Solution {    /*     * @param nums: a rotated sorted array     * @return: the minimum number in the array     */    public int findMin(int[] nums) {        // write your code here        int start = 0;        int end = nums.length - 1;        int cmpTarget = nums[end];        while (start + 1 < end){            int mid = start + (end - start) / 2;            if (nums[mid] > cmpTarget){                start = mid;            } else {                end = mid;            }        }        return Math.min(nums[start], nums[end]);    }}

 

转载于:https://www.cnblogs.com/jasminemzy/p/7580058.html

你可能感兴趣的文章
test1
查看>>
jquery ajax 局部table 刷新技术
查看>>
类的关联、组合、聚合关系
查看>>
binary hacks读数笔记(ld 链接讲解 二)
查看>>
SKPhysicsJoint类
查看>>
在Ubuntu下编译Qt错误及处理办法
查看>>
LVS-Keepalived高可用集群(DR)
查看>>
day3_python可变的类型、不可变的类型
查看>>
数据结构(3)-线性表顺序结构的合并操作
查看>>
6个html5页面适配iphone6的技巧
查看>>
Use Slim to overview model in Tensorflow like model.summary() in Keras
查看>>
《编写高质量代码--Web前端开发修炼之道》读书笔记
查看>>
Arduino超级马里奥游戏机
查看>>
Objective-C数组
查看>>
.net开源CMS
查看>>
你懂AI吗(1)
查看>>
双拼输入法
查看>>
CentOS7 中防火墙配置
查看>>
php扩展目录
查看>>
PageRank算法
查看>>