zoukankan      html  css  js  c++  java
  • ACM && Find Minimum in Rotated Sorted Array

     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/

  • 相关阅读:
    openfalcon源码分析之transfer
    openfalcon源码分析之hbs
    openfalcon源码分析之Judge
    kong插件官方文档翻译
    Lua 学习
    GO语言heap剖析及利用heap实现优先级队列
    GO语言list剖析
    算法之python创建链表实现cache
    杂项之rabbitmq
    杂项之python利用pycrypto实现RSA
  • 原文地址:https://www.cnblogs.com/wizzhangquan/p/4147168.html
Copyright © 2011-2022 走看看