154. Find Minimum in Rotated Sorted Array II

来源:互联网 发布:中国工业企业数据库 编辑:程序博客网 时间:2024/06/03 18:33

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 an array sorted in ascending order 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.

class Solution {public:    int findMin(vector<int>& nums) {          if(nums.size() == 0)              return 0;          if(nums.size() == 1)              return nums[0];          int low = 0, high = nums.size() - 1,mid;          while(low < high){              mid = (low + high) / 2;              if(nums[mid] < nums[high])                  high = mid;              else if(nums[mid] > nums[high] )                  low = mid + 1;            else                high --;            if(low == high)                  return nums[low];          }          return nums[mid];      }  };

原创粉丝点击