Leetcode

LeetCode代码分析—— 33. Search in Rot

2018-05-11  本文已影响0人  JackpotDC

题目描述

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]).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

Your algorithm's runtime complexity must be in the order of O(log n).

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4

Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1

有一个被旋转过的升序数组,例如原来是[0,1,2,4,5,6,7],从某个位置为轴旋转为[4,5,6,7,0,1,2],数组元素不重复,要求以log n的复杂度找到目标数字target。

思路分析

log n的复杂度,升序数组,可以联想到二分查找法,但是这里的数组是旋转过的,所以要考虑基于二分查找的思想进行变种。可以用到数学的中方法——分情况讨论。
基于二分查找,mid = (start + end) / 2;,mid的情况可以分为三种:

代码实现

class Solution {
    public int search(int[] nums, int target) {
        return find(nums, 0, nums.length - 1, target);
    }
    
    private int find(int[] nums, int start, int end, int target) {
        if (start > end) {
            return -1;
        }

        int mid = (start + end) / 2;

        if (nums[mid] == target) {
            return mid;
        }
        
        if (nums[start] == target) {
            return start;
        }

        if (nums[start] < nums[mid]) {
            if (target < nums[start] || target > nums[mid]) {
                return find(nums, mid + 1, end, target);
            } else {
                return find(nums, start, mid - 1, target);
            }
        } else if (nums[start] > nums[mid]) {
            if (target > nums[start] || target < nums[mid]) {
                return find(nums, start, mid - 1, target);
            } else {
                return find(nums, mid + 1, end, target);
            }
        } else {
            return find(nums, mid + 1, end, target);
        }
    }

}
上一篇 下一篇

猜你喜欢

热点阅读