lintcode:Find Minimum in Rotated Sorted Array

原创 2015年11月17日 21:09:42

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.

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

Note
You may assume no duplicate exists in the array.

class Solution {
public:
    /**
     * @param num: a rotated sorted array
     * @return: the minimum number in the array
     */
    int findMin(vector<int> &num) {
        // write your code here
        int l=0,r=num.size()-1;
        while(l<r-1){//
           int mid=l+(r-l)/2;
           if(num[mid]<num[r]){
               r=mid;
           }else{
               l=mid;
           }
        }

        if(num[l]<num[r]){
            return num[l];
        }else{
            return num[r];
        }

    }
};

这题说实话比较难。
关键在于要跟num[r]比较,跟num[l]比较比较复杂;
二、在于循环条件l< r-1

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【LeetCode-面试算法经典-Java实现】【154-Find Minimum in Rotated Sorted Array II(找旋转数组中的最小数字II)】

【154-Find Minimum in Rotated Sorted Array II(找旋转数组中的最小数字II)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Fo...

leetcode解题之153&154. Find Minimum in Rotated Sorted Array版(在旋转的数组中查找最小数字)

leetcode解题之153&154. Find Minimum in Rotated Sorted Array版(在旋转的数组中查找最小数字)...

LeetCode Solutions : Find Minimum in Rotated Sorted Array

【算法思路】利用折半查找的思路去查找这个最小元素 【编程步骤】 * 1. 如果数组num只有一个元素,则所求的最小的元素就是它了; * 2. 若left到right位置的元素严格递增,则最小的元素...
  • lviiii
  • lviiii
  • 2015年01月07日 15:35
  • 694

[leetcode] 154.Find Minimum in Rotated Sorted Array II

题目: Follow up for “Find Minimum in Rotated Sorted Array”: What if duplicates are allowed?Would thi...

153. Find Minimum in Rotated Sorted Array\229. Majority Element II\Moore's voting algorithm

Find Minimum in Rotated Sorted Array 题目描述 代码实现153. Find Minimum in Rotated Sorted Array题目描述一个已经排好序的数...

Leetcode 153. Find Minimum in Rotated Sorted Array

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

※ Leetcode - Binary Search - 153. Find Minimum in Rotated Sorted Array(二分查找)

1. Problem Description Suppose a sorted array is rotated at some pivot unknown to you beforehand.   ...

【leetcode】Find Minimum in Rotated Sorted Array

一、问题描述 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1...

[leetcode 153]Find Minimum in Rotated Sorted Array----求旋转数组的最小值

Question: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., ...

leetcode之Find Minimum in Rotated Sorted Array

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 m...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:lintcode:Find Minimum in Rotated Sorted Array
举报原因:
原因补充:

(最多只允许输入30个字)