leetcode 33. 搜索旋转排序数组

2020-11-05  本文已影响0人  Source_Chang

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;
    }
};
上一篇下一篇

猜你喜欢

热点阅读