[LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search

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

1) 这个题目思路就是先画图, 分区间讨论,注意是左移还是右移,最好画图判断下。

2)可以利用[LeetCode] 153. Find Minimum in Rotated Sorted Array_Medium tag: Binary Search 的思路,通过lg n 找到转折点,然后分别在两段里面找。

T: O(lgn)

Code

1)

class Solution:
    def search(self, nums, target):
        if not nums: return -1
        S, E, l, r = nums[0], nums[-1], 0, len(nums) - 1
        while l + 1 < r:
            mid = l + (r - l)//2
            if target == nums[mid]: return mid
            if nums[mid] >= S:
                if S <= target <= nums[mid]:
                    r = mid
                else:
                    l = mid
            else:
                if nums[mid] <= target <= E:
                    l = mid
                else:
                    r = mid
        if nums[l] == target: return l
        if nums[r] == target: return r
        return -1

原文地址:https://www.cnblogs.com/Johnsonxiong/p/10715364.html

时间: 2024-11-09 01:45:15

[LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search的相关文章

[Leetcode][JAVA] Convert Sorted Array to Binary Search Tree &amp;&amp; Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 很简单的二分法,只要给出Array的开始和结束下标作为参数传入即可. 1 public TreeNode sortedArrayToBST(int[] num) { 2 return constructBST(num,

Convert Sorted Array to Binary Search Tree &amp; Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 方法:将有序数组转为平衡二叉树,我们知道平衡二叉树的中序遍历是有序数组,而且“中间”的元素可以作为根节点,那么构建树的时候,找出中间元素,构造根元素,然后继续重复上面的方法,构造左子树和右子树.代码如下: 1 /**

34. Convert Sorted List to Binary Search Tree &amp;&amp; Convert Sorted Array to Binary Search Tree

Convert Sorted List to Binary Search Tree OJ: https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 思想: 以中间点为根节点,按先序顺序

154. Find Minimum in Rotated Sorted Array II(Binary search)

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/ -- leetcode follow up question: find the minimum element in a rotated array with duplicate elements Idea: [3,3,1,3] compared to [3,4,1,2]  -> l = mid+1 --1 [1,3,3] com

【LeetCode】109&amp; - Convert Sorted List to Binary Search Tree&amp;Convert Sorted Array to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. Solution 1:  recursion runtime: 28ms. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(i

LeetCode: Convert Sorted Array to Binary Search Tree &amp;&amp; Convert Sorted List to Binary Search Tree

Title: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Title: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 思路:自顶向下 /** * Definition for

Jan 23 - Search In Rotated Array II; Array; Binary Search;

public class Solution { public boolean search(int[] nums, int target) { int len = nums.length; if(len == 0) return false; int low = 0; int high = len-1; while(low < high){ int mid = (low+high)/2; // do not concern the integer overrange situation if(n

leetcode 109 Convert Sorted List to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Convert Sorted List to Binary Search Tree Description Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * De

leetcode 108 Convert Sorted Array to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Convert Sorted Array to Binary Search Tree Description Given an array where elements are sorted in ascending order, convert it to a height balanced BST. /** * Definition f