搜索旋转排序数组 II

来源:互联网 发布:淘宝好友在哪里看 编辑:程序博客网 时间:2024/05/18 02:44

描述

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

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

样例

给出[3,4,4,5,7,0,1,2]和target=4,返回 true

思考

  1. 直接 for 循环查找

代码

//  By Lentitudeclass 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        int len = A.size();        for (int i = 0; i != len; ++i){            if (A[i] == target)            return true;        }        return false;    }};
0 0
原创粉丝点击