标签:代码 find c++ 最小值 java tco 题目 一个 vector
一:解题思路
这个题目是二分搜索的一个变体。Time:log(n),Space:O(1)
二:完整代码示例 (C++版和Java版)
C++:
class Solution { public: int findMin(vector<int>& 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 (low<high) { int mid=low+(high-low)/2; if(nums[mid]>nums[high]) low=mid+1; else high=mid; } return nums[low]; } }
标签:代码 find c++ 最小值 java tco 题目 一个 vector
原文地址:https://www.cnblogs.com/repinkply/p/12737067.html