二分法
class Solution {
public:
int mySqrt(int x) {
int l = 0, r = x, ans = -1;
while (l <= r) {
int mid = l + (r - l) / 2;
if ((long long)mid * mid <= x) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return ans;
}
};
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int Length=nums.size();
int left=0;
for(int right=0;right<Length;right++)
{
if(nums[right]!=val)
{
nums[left]=nums[right];
left++;
}
}
return left;
}
};
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int left=0;
int right=nums.size();
int mid=0;
while(left<right)
{
mid=left+(right-left)/2;
if(nums[mid]>target)
{
right=mid;
}
else if(nums[mid]<target)
{
left=mid+1;
}
else
{
return mid;
}
}
return right;
}
};