Find Minimumd in Rotated Sorted Array

二分搜索查最小数,from mid to分别为区间的第一个,中位数,和最后一个数

if(from<=mid&&mid<=to)//顺序,第一个即为最小值

return from;

if(from>mid)//发现逆序,则最小值在这个区间,2分搜索区间

to = mid;

if(mid>to)//发现逆序,则最小值在这个区间,2分搜索区间

from = mid;

代码如下:

 1 class Solution {
 2 public:
 3
 4     int findMin(vector<int> &num) {
 5         int size = num.size();
 6         if(size == 0)
 7             return 0;
 8         if(size==1)
 9             return num[0];
10         int from = 0;
11         int to = size-1;
12
13         for(int i=0;i<size;i++)
14         {
15             int mid = (from+to)/2;
16             if(num[from]<=num[mid]&&num[mid]<=num[to])
17             return num[from];
18             else if(num[from]>num[mid])
19             {
20                 if(from+1 == mid)
21                     return num[mid];
22               to = mid;
23             }
24             else if(num[mid]>num[to])
25             {
26                 if(mid+1==to)
27                     return num[to];
28                  from = mid;
29             }
30         }
31
32     }
33 };
时间: 2024-08-04 10:31:24

Find Minimumd in Rotated Sorted Array的相关文章

[leetcode]Search in Rotated Sorted Array II

问题描述: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. "Search in Rotated Sorted Array&q

LeetCode: Search in Rotated Sorted Array

LeetCode: Search 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 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, othe

【Lintcode】062.Search 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 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

LeetCode33 Search 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 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

Leetcode[154]-Find Minimum in Rotated Sorted Array II

Link: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is

[LeetCode] Search in Rotated Sorted Array I (33) &amp;&amp; II (81) 解题思路

33. Search 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 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise

[leedcode 154] Find Minimum in Rotated Sorted Array II

Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might be

LeetCode Find Minimum in Rotated Sorted Array II

Find Minimum in Rotated Sorted Array II Total Accepted: 23090 Total Submissions: 73108 My Submissions Question Solution Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexi

【leetcode】Search in Rotated Sorted Array II

Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 与I类似,只是在二分搜