题解 | #二分查找-I#
二分查找-I
http://www.nowcoder.com/practice/d3df40bd23594118b57554129cadf47b
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型vector
* @param target int整型
* @return int整型
*/
int search(vector& nums, int target) {
int i=0, j =nums.size()-1;
if(j<0)
return -1;
while(i <= j )
{
int n = (i+j)/2;
if(nums[n] < target)
{
i = n+1;
}
else if(nums[n] > target)
{
j = n-1;
}
else
return n;
}
return -1;
// write code here
}
};