leetcode 033 Search in Rotated Sorted Array

35 篇文章 0 订阅
35 篇文章 0 订阅

题目

Suppose a sorted array 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.

思路:

题目意思是:有一个已经排好序的数组,但是这个数组从中间某一个点旋转了一下。给一个数,查找它在这个数组中的位置。
我的算法是:先找出pivot所在位置,然后然后比较数组第一个数和target的大小,若nums[0] <= target,则在0至piovt+1之间用二分查找;否则在pivot+1至最后用二分查找。
说实话,我不明白这题目为什么会是hard级别的,就用最简单的直接查找也能在1ms完成。

代码:

public int search(int[] nums, int target)
{
    int pivot = 0;
    while(pivot < nums.length - 1 && nums[pivot] < nums[pivot + 1])
        pivot++;
    int res;
    if(nums[0] <= target)
        res = Arrays.binarySearch(nums,0,pivot+1,target);
    else
        res = Arrays.binarySearch(nums,pivot+1,nums.length,target);
    return res < 0 ? -1 : res;
}

结果细节(图):

image

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值