p148 旋转有序数组的最小值(leetcode 153)

2021-02-11 11:18

阅读:475

标签:代码   find   c++   最小值   java   tco   题目   一个   vector   

一:解题思路

这个题目是二分搜索的一个变体。Time:log(n),Space:O(1)

二:完整代码示例 (C++版和Java版)

C++:

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

        return nums[low];
    }
};

Java:

class Solution {
        public int findMin(int[] nums) 
        {
               int low=0;
               int high=nums.length-1;
               while (lowhigh)
               {
                   int mid=low+(high-low)/2;
                   if(nums[mid]>nums[high]) low=mid+1;
                   else high=mid;
               }
               
               return nums[low];
        }
    }

 

p148 旋转有序数组的最小值(leetcode 153)

标签:代码   find   c++   最小值   java   tco   题目   一个   vector   

原文地址:https://www.cnblogs.com/repinkply/p/12737067.html


评论


亲,登录后才可以留言!