LintCode : Find Minimum in Rotated Sorted Array
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LintCode : Find Minimum in Rotated Sorted Array相关的知识,希望对你有一定的参考价值。
public class Solution { /** * @param num: a rotated sorted array * @return: the minimum number in the array */ public int findMin(int[] num) { // write your code here // handle corner case // the idea is to find the [first] element less than num[num.length - 1] if (num == null || num.length == 0) { return -1; } int start = 0, end = num.length - 1; int target = num[end]; while (start + 1 < end) { int mid = start + (end - start) / 2; int current = num[mid]; if (current > target) { start = mid; } else { end = mid; } } // decide which to choose if (num[start] < target) { return num[start]; } return num[end]; } }
使用二分搜索来做,基本思想就是: find the first element less than num[num.length - 1],至于为什么要这样,画个图就知道了后一段的数肯定要比前一段的数小。
以上是关于LintCode : Find Minimum in Rotated Sorted Array的主要内容,如果未能解决你的问题,请参考以下文章
lintcode159- Find Minimum in Rotated Sorted Array- medium
lintcode160- Find Minimum in Rotated Sorted Array II- medium
LintCode : Find Minimum in Rotated Sorted Array
[Leetcode + Lintcode] 162. Find Peak Element