剑指 offer 旋转数组的问题

目前的话,只是做到了 三分之一的通过率。

class Solution {
public:
    int minNumberInRotateArray(vector<int> rotateArray) {
        if(rotateArray.empty()){
            return 0;
        }
        int index1 = 0;
        int index2 = rotateArray.size()-1;
        int midindex = index1;
        while (rotateArray[index1] > rotateArray[index2]){
            midindex = (index1 + index2 ) /2;
            if (index2 - index1 ==1){
                 midindex = index2;
                break;
            }
            else if (rotateArray[index1] == rotateArray[index2] && 
                     rotateArray[index1] == rotateArray[midindex]){
                return minorder(rotateArray,index1,index2);
            }
            else if (rotateArray[midindex] > rotateArray[index2]){
               index1 = midindex ;
            }
            else if (rotateArray[midindex] <= rotateArray[index2]){
               index2 = midindex ;
            }             
        }
        return rotateArray[midindex];
        
    }
    int minorder(vector<int> rotateArray,int index1,int index2){
         int temp = rotateArray[index1];
         for (int i =index1+1; i<=index2;i++){
             if (rotateArray[i]<temp){
                 temp = rotateArray[i];
             }
         }
        return temp;
        
        
      }
};
发布了95 篇原创文章 · 获赞 8 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/ttomchy/article/details/104594220