LintCode : 搜索旋转排序数组 II

LintCode : 搜索旋转排序数组 II

跟进“搜索旋转排序数组”,假如有重复元素又将如何?

是否会影响运行时间复杂度?

如何影响?

为何会影响?

写出一个函数判断给定的目标值是否出现在数组中。

方案一

遍历所有元素,时间复杂度O(n).


class Solution {
    /** * param A : an integer ratated sorted array and duplicates are allowed * param target : an integer to be search * return : a boolean */
public:
    bool search(vector<int> &A, int target) {
        // write your code here
        if (A.size() == 0) return false;
        for (int i=0; i<A.size(); i++){
            if (A[i] == target) return true;
        }
        return false;
    }
};

方案二: 二分法

你可能感兴趣的:(lintcode)