码迷,mamicode.com
首页 > 其他好文 > 详细

ACM && Find Minimum in Rotated Sorted Array

时间:2014-12-05 19:07:46      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

 1 /*Find Minimum in Rotated Sorted Array */
 2 #include <iostream>
 3 #include <vector>
 4 using namespace std;
 5 
 6 int find(vector <int>& num, int begin, int end) ;
 7 int findMin(vector<int> &num) {
 8     cout<<num.size();
 9     return find(num, 0,  num.size()-1 );
10 }
11 
12 int find(vector<int> &num, int begin, int end) {
13     cout<<"begin: "<<num[begin]<<"  end: "<<num[end]<<endl;
14     if(begin==end)
15         return num[end];
16     if(begin==end-1)
17         return num[begin]>num[end]?num[end]:num[begin];
18     if(num[begin]<num[end])
19         return num[begin];
20     int mid=(begin+end)/2;
21     cout<<"mid:"<<num[mid]<<endl;
22     if(num[mid]>num[begin])
23         return find(num, mid+1, end);
24     else if(num[mid]<num[begin])
25         return find(num,begin, mid);
26     else {
27         int a,b;
28         a=find(num, begin, mid);
29         b=find(num, mid, end);
30         return a>b?b:a;
31     }
32 }
33 
34 void main(){
35     int a[3]={3,1,1};
36     vector<int> num(a, a+3);
37     int c=findMin(num);
38     cout<<c<<endl;
39     while(1);
40 }

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 become 4 5 6 7 0 1 2).

Find the minimum element.

The array may contain duplicates.

https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/

ACM && Find Minimum in Rotated Sorted Array

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/wizzhangquan/p/4147168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!