C++, My solution, easy to understand


  • 0
    C
    /****
     ****author: cxq
     ****weibo: http://weibo.com/chenxq1992
     ****/
    
     class Solution {
        public:
            int findMin(vector<int>& nums) {
                if (nums.empty()) return 0;
                int lo = 0, hi = nums.size() - 1;
                int result = nums[hi];
                while (lo <= hi) {
                    int mid = lo + (hi - lo) / 2;
                    if (nums[mid] > nums[lo]) {   // that means range[lo, mid] is by ascending order
                        result = min(nums[lo], result);
                        lo = mid + 1;
                    }
                    else if (nums[mid] < nums[lo]) {    // that means nums[mid] is in smaller range
                        result = min(nums[mid], result);
                        hi = mid - 1;
                    } else {                                         // deplicate we will pass
                        result = min(result, nums[mid]);
                        lo++;
                    }
                        
                }
                
                return result;
            }
        };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.