leetcode解题之153&154. Find Minimum in Rotated Sorted Array版(在旋转的数组中查找最小数字)

来源:互联网 发布:全民枪战挂机软件 编辑:程序博客网 时间:2024/04/29 23:04

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.

给定一个数组,数组可能被旋转了,但是在旋转之前是递增有序的,找出该数组的的最小值。(数组没有重复值

复杂度等价于一个二分查找,是O(logn),空间上只有四个变量维护二分和结果,所以是O(1)。

采用二分查找的方法进行查找。数组的第一个元素要大于最后一个元素,因为数组是递增的,如果不小于则第一个元素就是最小的元素。如果不小于,初始化high=0,high=len-1,则取中间数mid,如果中间数大于high指向的元素,则说明小数存在后半段,则将high指向mid,否则将high指向mid,继续进行寻找。

只需要判断是否为递增序列或者是否只有一个元素特殊情况即可,添加一句,if (nums[low] <= nums[high]) return nums[low];继续二分查找。等号判断只有一个元素情况,小于判断递增序列。注意:如果中间元素小于最左边元素,则右部分为有序数组。说明此时中间元素已经在最小值右侧,不然不会小于最左侧元素

154. Find Minimum in Rotated Sorted Array II

Follow up for "Find Minimum in Rotated Sorted Array":
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

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.

The array may contain duplicates.

给定一个数组,数组可能被旋转了,但是在旋转之前是递增有序的,找出该数组的的最小值。(数组有重复值

两道题解法一样,有重复值只要判断 low high mid三者指向的元素是否相等,相等就退化为O(N)复杂度。

public int search(int[] nums, int target) {if (nums == null || nums.length < target)return 0;int low = 0;int res = 0;int high = nums.length - 1;if (nums[low] <= nums[high])res = Arrays.binarySearch(nums, target);else {int m = findMin(nums);System.out.println(m);res = Arrays.binarySearch(nums, 0, m, target);if (res < 0)res = Arrays.binarySearch(nums, m, high + 1, target);}if (res < 0)return -1;elsereturn res;}public int findMin(int[] nums) {// ask interviewer which value should return:// Integer.MIN_VALUE or throw a Exception.if (nums == null || nums.length == 0)return -1;int low = 0;int mid = 0;int high = nums.length - 1;while (nums[low] >= nums[high]) {// 表示找到if (high - low == 1) {mid = high;break;}mid = (low + high) / 2;// 如果三个数字相等需要遍历整个数字查找最小值if (nums[mid] == nums[low] && nums[mid] == nums[high])return MinOrder(nums, low, high);// 处于递增子序列中,最小值在右侧if (nums[mid] >= nums[low])low = mid;else// 处于递减子序列中,最小值在左侧high = mid;}return nums[mid];}// 遍历整个数组求最小值private int MinOrder(int[] nums, int low, int high) {int min = nums[low];for (int i = low + 1; i <= high; i++)min = Math.min(min, nums[i]);return min;}
网上参考:http://www.programcreek.com/2014/03/leetcode-find-minimum-in-rotated-sorted-array-ii-java/
public class Solution {   public int findMin(int[] num) {    return findMin(num, 0, num.length-1);} public int findMin(int[] num, int left, int right){    if(right==left){        return num[left];    }    if(right == left +1){        return Math.min(num[left], num[right]);    }    // 3 3 1 3 3 3     int middle = (right-left)/2 + left;    // already sorted    if(num[right] > num[left]){        return num[left];    //right shift one    }else if(num[right] == num[left]){        return findMin(num, left+1, right);    //go right        }else if(num[middle] >= num[left]){        return findMin(num, middle, right);    //go left        }else{        return findMin(num, left, middle);    }}





0 0
原创粉丝点击