[Leetcode] [Python]33:search in rotated Sorted Array

Source: Internet
Author: User

#-*-Coding:utf8-*-
‘‘‘
__author__ = ' [email protected] '

33:search in rotated Sorted Array
https://oj.leetcode.com/problems/search-in-rotated-sorted-array/

Suppose a sorted array is rotated on 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 is given a target value to search. If found in the array is return its index, otherwise return-1.
Assume no duplicate exists in the array.

===comments by dabay===
Solution One: (Can over OJ)
Start by judging whether you should go from the back or back.
Depends on and the first number of judgments, if smaller than the first number, from the back forward, if larger than the first number, the past.
Note Some conditions for continuing the loop:
In the past: the subscript is effective, incremental, and the target is larger than the previous
From the back: the subscript is effective, diminishing, and the target is smaller than the previous

Solution Two: (Two-point search)
Mark the median number.
If the median is larger than the left side, the left side is incremented, and the breakpoint is on the right:
If target increments the range on the left, it looks on the left;
Otherwise, find on the right
If the median is smaller than the left side, the right side is incremented, and the breakpoint is on the left:
If Target is in the increment interval on the right, it is located on the right;
Otherwise, find on the left.

From the results of running, for small data, the solution is a little bit faster.
‘‘‘

Class Solution:
# @param A, a list of integers
# @param target, an integer to be searched
# @return An integer
def search (self, A, target):
Left, right = 0, Len (A)-1
While left <= right:
If a[left] = = target:
return left
If a[right] = = target:
return right
M = (left + right)/2
MID = A[m]
If mid = = target:
return m
If mid > A[left]:
If a[left]< target and Target < A[M]:
right = M-1
Else
left = m + 1
Else
If A[M] < target and Target < A[right]:
left = m + 1
Else
right = M-1
Else
Return-1


# def search (self, A, target):
# If Len (A) = = 1:
# return [-1, 0][a[0]==target]
#
# if a[0] = = target:
# return 0
# elif A[0] < target:
# i = 1
# While I < Len (A) and a[i-1] < A[i] and A[i-1] < target:
# if a[i] = = target:
# return I
# i = i + 1
# Else:
# return-1
# Else:
# if a[-1] = = target:
# return Len (A)-1
# i = Len (A)-2
# while I >= 0 and A[i] < a[i+1] and Target < a[i+1]:
# if a[i] = = target:
# return I
# i = i-1
# Else:
# return-1


def main ():
s = solution ()
Nums = [4,5,6,7,8,1,2,3]
Print S.search (nums, 8)


if __name__ = = "__main__":
Import time
Start = Time.clock ()
Main ()
Print "%s sec"% (Time.clock ()-start)


[Leetcode] [Python]33:search in rotated Sorted Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.