LEETCODE: Search in Rotated Sorted Array II

来源:互联网 发布:比特币技术指标软件 编辑:程序博客网 时间:2024/05/22 10:51

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 left = 0;        int right = n - 1;        while(left <= right) {            int mid = (left + right) / 2;            if(A[mid] == target) return true;            if(A[left] == A[mid]) {                left ++;                continue;            }            if(A[right] == A[mid]) {                right --;                continue;            }            if(A[left] < A[mid] ) {if(target < A[mid]  && target >= A[left])right = mid - 1;elseleft = mid + 1;            }            else { if(target <= A[right] && target > A[mid]) left = mid + 1;elseright = mid - 1;            }        }        return false;    }};




0 0
原创粉丝点击