Leetcode_search-in-rotated-sorted-array-ii(updated c++ and python version)
2014-05-10 11:10
579 查看
地址:http://oj.leetcode.com/problems/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.
思路:旋转后的是两个升序的子序列,先找到”断点“,然后根据第一个数,判断在哪一个有序的子数组里做二分查找。
C++ 参考代码:
O(n) running time, second trial is better
python参考代码:
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.
思路:旋转后的是两个升序的子序列,先找到”断点“,然后根据第一个数,判断在哪一个有序的子数组里做二分查找。
C++ 参考代码:
O(n) running time, second trial is better
class Solution { public: bool binary_search(int A[], int n, int target) { int left = 0, right = n-1, mid = 0; while(left<=right) { mid = (left+right)/2; if(target < A[mid]) right = mid-1; else if(target > A[mid]) left = mid+1; else return true; } return false; } bool search(int A[], int n, int target) { if(!n) return false; if(n==1) return A[0] == target; int pos = 0; while(pos<n-1 && A[pos]<=A[pos+1]) ++pos; if(target > A[0]) return binary_search(A, pos+1, target); else if(target < A[0]) return binary_search(A+pos+1, n-pos-1, target); else return true; } };
python参考代码:
class Solution: def bin_search(self, arr, target): left = 0 right = len(arr)-1 while left <= right: mid = (left+right)//2 if target < arr[mid]: right = mid-1 elif target > arr[mid]: left = mid+1 else: return True return False # @param A a list of integers # @param target an integer # @return a boolean def search(self, A, target): if not A: return False i = 0 while i<len(A)-1 and A[i]<=A[i+1]: i+=1 if target > A[0]: return self.bin_search(A[:i+1], target) elif target < A[0]: return self.bin_search(A[i+1:], target) else: return True
//SECOND TRIAL, average running time O(lgn), worst time O(n)class Solution {private: bool rotatedSearch(int A[], int left, int right, int target) { if(!A || left > right) return false; int mid = (left+right)/2; if(A[mid]==target || A[left]==target || A[right]==target) return true; if(A[left]<A[mid]) { if(target>A[left] && target<A[mid] && rotatedSearch(A, left+1, mid-1, target)) return true; return rotatedSearch(A, mid+1, right-1, target); } else if(A[left]>A[mid]) { if(target>A[mid] && target<A[right] && rotatedSearch(A, mid+1, right-1, target)) return true; return rotatedSearch(A, left+1, mid-1, target); } else { if(rotatedSearch(A, left+1, mid-1, target)) return true; return rotatedSearch(A, mid+1, right-1, target); } }public: bool search(int A[], int n, int target) { return rotatedSearch(A, 0, n-1, target); }};
相关文章推荐
- 81. Search in Rotated Sorted Array II Leetcode Python
- [C++]LeetCode: 44 Search in Rotated Sorted Array II
- LeetCode之Search in Rotated Sorted Array II ()C++
- [LeetCode]Search in Rotated Sorted Array II@python
- leetcode 【 Search in Rotated Sorted Array II 】python 实现
- [leetcode]@python 81. Search in Rotated Sorted Array II
- LeetCode 33 — Search in Rotated Sorted Array(C++ Java Python)
- leetcode刷题系列C++-Search in Rotated Sorted Array II
- Leetcode_remove-duplicates-from-sorted-array-ii (c++ and python version)
- Leetcode_find-minimum-in-rotated-sorted-array-ii(c++ version)
- Leetcode_merge-sorted-array(updated c++ and python version)
- 【LEETCODE】81-Search in Rotated Sorted Array II [Python]
- [leetcode]Search in Rotated Sorted Array II @ Python
- [LeetCode]题解(python):081-Search in Rotated Sorted Array II
- 【Leetcode】【python】Search in Rotated Sorted Array/Search in Rotated Sorted Array II
- LeetCode 之 Search in Rotated Sorted Array I II — C/C++ 实现
- Leetcode_c++: Search in Rotated Sorted Array II (081)
- leetcode刷题系列C++-Search in Rotated Sorted Array II
- [leetcode] Search in Rotated Sorted Array II
- [LeetCode] Search in Rotated Sorted Array II