美文网首页
Lintcode62 Search in Rotated Sor

Lintcode62 Search in Rotated Sor

作者: 代码码着玩 | 来源:发表于2017-05-02 11:42 被阅读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.

    假设有一个排序的按未知的旋转轴旋转的数组(比如,0 1 2 4 5 6 7 可能成为4 5 6 7 0 1 2)。给定一个目标值进行搜索,如果在数组中找到目标值返回数组中的索引位置,否则返回-1。你可以假设数组中不存在重复的元素。

    【题目链接】

    www.lintcode.com/en/problem/search-in-rotated-sorted-array/

    【题目解析】

    如果没有重复元素,就是二分搜索,mid要么落在大元素的区间,要么落在小元素的区间。先看mid落在前半区间还是后半区间,再看target是在该区间的前半部分还是后半部分。

    【参考答案】

    www.jiuzhang.com/solutions/search-in-rotated-sorted-array/

    相关文章

      网友评论

          本文标题:Lintcode62 Search in Rotated Sor

          本文链接:https://www.haomeiwen.com/subject/aatjottx.html