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;
}
};
网友评论