81. Search in Rotated Sorted Array II Leetcode Python

CSDN学院讲师招募,,诚邀您加入!博客Markdown编辑器全新体验PMBOK第五版精讲视频教程读文章说感想 获好礼火星人敏捷开发1001问

81. Search in Rotated Sorted Array II Leetcode Python

分类:leetcode

Follow up for "Search in Rotated Sorted Array":What ifduplicatesare allowed?

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

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

generally we can have two methods to do this problem, the first one is based on finding pivot and then divide the array into two parts, then search.

second one is still based on Binary search.

class Solution:# @param A a list of integers# @param target an integer# @return a booleandef search(self, A, target):if len(A) == 0:return Falselow = 0high = len(A) – 1while low < high:mid = low + (high – low) / 2if A[mid] == target:return Trueif A[low] < A[mid]:if A[low] <= target < A[mid]:high = mid – 1else:low = mid + 1elif A[low] > A[mid]:if A[mid]< target <= A[high]:low = mid +1else:high = mid -1else:low += 1if A[low] == target:return Truereturn False

上一篇topological sort python recursive and iterative下一篇166. Fraction to Recurring Decimal Leetcode Python

顶0踩0

主题推荐猜你在找

查看评论

* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

核心技术类目

未经一番寒彻骨,焉得梅花扑鼻香

81. Search in Rotated Sorted Array II Leetcode Python

相关文章:

你感兴趣的文章:

标签云: