33. Search in Rotated Sorted Array && 81. Search in Rotated Sorted Array II &&

33. Search in Rotated Sorted Array

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.

Hide Tags

Binary Search Array

Hide Similar Problems

(M) Search in Rotated Sorted Array II (M) Find Minimum in Rotated Sorted Array

public class Solution {
  public int search(int[] nums, int target) {
    int len = nums.length;
    int leftMin = nums[0];
    int rightMax = nums[len - 1];

    if (target > rightMax && target < leftMin)
      return -1;
    boolean targetAtLeft = target >= leftMin;

    int left = 0;
    int right = len - 1;

    while (left <= right) {
      int mid = (left + right) / 2;
      int midVal = nums[mid];
      if (midVal == target)
        return mid;
      if (targetAtLeft) {
        if (midVal >= leftMin && midVal < target)
          left = mid + 1; // go right
        else
          right = mid - 1; //go left
      } else {
        if (midVal <= rightMax && midVal > target)
          right = mid - 1; // go left
        else
          left = mid + 1; //go right
      }
    }
    return -1;
  }
}

81. Search in Rotated Sorted Array II

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

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

Write a function to determine if a given target is in the array.

Hide Tags

Array Binary Search

Hide Similar Problems

(H) Search in Rotated Sorted Array

时间: 2024-12-25 14:09:24

33. Search in Rotated Sorted Array && 81. Search in Rotated Sorted Array II &&的相关文章

LeetCode 33 Search in Rotated Sorted Array [binary search] &lt;c++&gt;

LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前半部分接到后半部分后面,得到一个新数组,在新数组中查找给定数的下标,如果没有,返回-1.时间复杂度限制\(O(log_2n)\) C++ 我的想法是先找到数组中最大值的位置.然后以此位置将数组一分为二,然后在左右两部分分别寻找target. 二分寻找最大值的时候,因为左半部分的数一定大于nums[l],所以n

81. Search in Rotated Sorted Array II

Follow up for "Search 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

LeetCode 5题 --Array+Binary search

这5道题都是array的binary search就可以搞定了 分别是leetcode(35)--Search Insert Position  leetcode(33)--Search in Rotated Sorted Array  leetcode(81)--Search in Rotated Sorted Array II  leetcode(34)--Search for a Range  leetcode(74)--Search a 2D Matrix 一:leetcode(35)-

有序线性搜索(Sorted/Ordered Linear Search)

如果数组元素已经排过序(升序),那我们搜索某个元素就不必遍历整个数组了.在下面给出的算法代码中,到任何一点,假设当前的arr[i]值大于搜索的值data,就可以停止搜索了. #include<stdio.h> // a function to search "data" in an array "arr" of size "size" // returns 1 if the element is present else 0 int

OPEN INFOREB SEARCH QUERY LOGSMATION EXTRACTION FROM WEB SEARCH QUERY LOGS

OPEN INFOREB SEARCH QUERY LOGSMATION EXTRACTION FROM WEB SEARCH QUERY LOGS 第一章  介绍 搜索引擎日益比传统的关键字输入.文档输出的先进,通过关注面向用户的任务提高用户体验,面向用户的任务包括查询建议.搜索个性化.推荐链接.这些以用户为中心的任务被从search query logs挖掘数据支撑.事实上,查询日志抓住用户对世界的认知,是这项应用的关键. 从查询日志中抽取的语言知识,如实体和关系,对上面的应用来说有很大的价

81. Search in Rotated Sorted Array II (Array; Divide-and-Conquer)

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 思路:此时可能存在nums[start]=nums[end]或者nums[start]=n

leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 思路:此题在解的时候,才发现Search in Rotated Sorted Array

【LeetCode】81. Search in Rotated Sorted Array II

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 这题跟上一题一样,都是二分查找法,但是需要思考,对于时间复杂度,存在重复元素会有影

[leedcode 81] Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. public class Solution { public boolean search