[leetcode]153. Find Minimum in Rotated Sorted Array@Java解题报告

来源:互联网 发布:蒙泰软件下载官方网站 编辑:程序博客网 时间:2024/06/05 07:07

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/


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.



思路:就是普通的二叉搜索

package go.jacob.day815;public class Demo2 {public int findMin(int[] nums) {if (nums == null || nums.length < 1)return -1;return findMin(nums, 0, nums.length - 1);}private int findMin(int[] nums, int left, int right) {if (left >= right)return nums[left];int mid = left + (right - left) / 2;if (nums[left] > nums[mid])return findMin(nums, left + 1, mid);if (nums[mid] > nums[right])return findMin(nums, mid + 1, right);return nums[left];}}


阅读全文
0 0
原创粉丝点击