0035 Search Insert Position
Simple
C++
问题概述
解题思路
class Solution {
public:
int searchInsert(std::vector<int>& nums, int target)
{
int NumsLeft = 0;
int NumsRight = (int)nums.size() - 1;
while (NumsLeft <= NumsRight)
{
int NumsMid = NumsLeft + (NumsRight - NumsLeft) / 2;
if (nums[NumsMid] == target) { return NumsMid; }
else if (nums[NumsMid] > target) { NumsRight = NumsMid - 1; }
else { NumsLeft = NumsMid + 1; }
}
return NumsLeft;
}
};