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

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

O(logN)

There are two section: 

1) increase array which the first element is larger than end one.

2) the minimum to end

Comparing with the endVal,

1) if a val is larger than endVal, it falls into the first section. Throw array. 

2) if a val is less or equal to end, it falls into the second section. Because we want to get the minimum, so we just need let end equal mid.

Always choose the bench mark in the "keep" section, in this problem, it is the endVal. Because there is a situation which it is not rotated.  

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        if (nums == null || nums.length == 0) {            return -1;        }                int start = 0;        int end = nums.length - 1;        int endVal = nums[end];                while(start + 1 < end) {            int mid = start + (end - start) / 2;            if (nums[mid] > endVal) {                start = mid;            } else {                end = mid;            }        }                if (nums[start] <= nums[end]) {            return nums[start];        }        if (nums[end] < nums[start]) {            return nums[end];        }                return -1;    }}

 

转载于:https://www.cnblogs.com/codingEskimo/p/6805068.html

你可能感兴趣的文章
解决svn异常报错“”cleanup failed to process the following paths …… previous operation has not finished”...
查看>>
富文本框--FreeTextBox的使用
查看>>
koa2使用阿里云oss的nodejs sdk实现上传图片
查看>>
简单select(2)
查看>>
pandas基础学习
查看>>
论文阅读笔记四十一:Very Deep Convolutional Networks For Large-Scale Image Recongnition(VGG ICLR2015)...
查看>>
用实例一步步教你写Jquery插件
查看>>
Qt资源整理ING
查看>>
看看checksec
查看>>
1. Two Sum
查看>>
MySQL基础之 标准模式通配符
查看>>
聊一聊python的单例模式
查看>>
mysql 8.0~MGR多成员读一致性
查看>>
python基础知识~list详解
查看>>
php表单提交时获取不到post数据的解决方法
查看>>
Smart3D系列教程6之 《案例实战演练3——倾斜数据正射影像及DSM的生产》
查看>>
面向对象特征
查看>>
android-23 View.java - dispatchTouchEvent源码
查看>>
08-JavaScript中的函数
查看>>
angularjs系列之双向绑定
查看>>