本文主要是介绍leetcode解题方案--153--Find Minimum in Rotated Sorted Array,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目
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.
You may assume no duplicate exists in the array.
分析
这道题和30多题差不对,用二分法查找边界值,代码很简单。
有递归和循环两种方法。
看来循环的内存效率和时间效率比递归好很多。
class Solution {public static int findMin(int[] nums) {int low = 0;int high = nums.length-1;if (nums[high] > nums[low]) {return nums[low];}while (high - low >1) {int mid = (high + low)/2;if (nums[mid] < nums[high]) {high = mid;}else {low = mid;}}return nums[high];}
}
这篇关于leetcode解题方案--153--Find Minimum in Rotated Sorted Array的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!