美文网首页
leetcode 33. 搜索旋转排序数组

leetcode 33. 搜索旋转排序数组

作者: SourceZhang | 来源:发表于2020-11-05 16:03 被阅读0次

leetcode

C++:

class Solution {
public:
    int search(vector<int>& nums, int target) {

        int left = 0;
        int right = nums.size() - 1;
        while ( left <= right ) {

            if ( nums[left] == target ) {

                return left;
            }

            if ( nums[right] == target ) {

                return right;
            }

            if ( nums[left] < target ) {

                ++left;

            } else if ( nums[right] < target ) {

                --right;

            } else {

                ++left;
                --right;
            }
        }

        return -1;
    }
};

相关文章

网友评论

      本文标题:leetcode 33. 搜索旋转排序数组

      本文链接:https://www.haomeiwen.com/subject/ockpvktx.html