LeetCode OJ Search in Rotated Sorted Array II

来源:互联网 发布:淘宝怎么卖二手闲置 编辑:程序博客网 时间:2024/06/15 07:28

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.

class Solution {public:    bool search(int A[], int n, int target) {        int l = 0, r = n - 1;        while (l <= r) {            int m = l + (r - l) / 2;            if (A[m] == target) return true;            if (A[l] < A[m]) {                if (A[l] <= target && target < A[m]) r = m - 1;                else l = m + 1;            } else if (A[l] > A[m]) {                if (A[m] < target && target <= A[r]) l = m + 1;                else r = m - 1;            } else {l++;}        }        return false;    }};


0 0