704.二分查找
题目链接:. - 力扣(LeetCode)
题目讲解:代码随想录
1)左闭右闭
时间复杂度O(logn) ,空间复杂度O(1)
class Solution {
public int search(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while(left <= right){
int middle = left + ((right - left) / 2);
if(nums[middle] < target) {
left = middle + 1;
}else if(nums[middle] > target){
right = middle - 1;
}else{
return middle;
}
}
return -1;
}
}
2)左闭右开
时间复杂度O(logn) ,空间复杂度O(1)
class Solution {
public int search(int[] nums, int target) {
int left = 0;
int right = nums.length;
while(left < right){
int middle = left + ((right - left) / 2);
if(nums[middle] < target) {
left = middle + 1;
}else if(nums[middle] > target){
right = middle;
}else{
return middle;
}
}
return -1;
}
}
35.搜索插入位置
题目链接:. - 力扣(LeetCode)
题目讲解:代码随想录
最优:
class Solution {
public int searchInsert(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while(left <= right){
int middle = left + ((right - left) / 2);
if(nums[middle] < target){
left = middle + 1;
}else if(nums[middle] > target){
right = middle - 1;
}else{
return middle;
}
}
return left;
}
}
暴力:
class Solution {
public int searchInsert(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while(left <= right){
int middle = left + ((right - left) / 2);
if(nums[middle] < target){
left = middle + 1;
}else if(nums[middle] > target){
right = middle - 1;
}else{
return middle;
}
}
int i = 0;
while(i < nums.length){
if(nums[i] >= target){
return i;
}
i++;
}
return nums.length;
}
}
34. 在排序数组中查找元素的第一个和最后一个位置
题目链接:. - 力扣(LeetCode)
题目讲解:代码随想录
暴力
class Solution {
public int[] searchRange(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
int[] ans = {-1,-1};
while(left <= right){
int middle = left + ((right - left) / 2);
if(nums[middle] < target){
left = middle + 1;
}else if(nums[middle] > target){
right = middle - 1;
}else{
//向前找
for(int i = middle;i >= 0;i--){
if(nums[i] < target){
ans[0] = i + 1;
break;
}
}
//向后找
for(int i = middle;i < nums.length;i++){
if(nums[i] > target){
ans[1] = i - 1;
break;
}
}
return ans;
}
}
return ans;
}
}
最优:
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int leftBorder = getLeftBorder(nums, target);
int rightBorder = getRightBorder(nums, target);
// 情况一
if (leftBorder == -2 || rightBorder == -2) return {-1, -1};
// 情况三
if (rightBorder - leftBorder > 1) return {leftBorder + 1, rightBorder - 1};
// 情况二
return {-1, -1};
}
private:
int getRightBorder(vector<int>& nums, int target) {
int left = 0;
int right = nums.size() - 1;
int rightBorder = -2; // 记录一下rightBorder没有被赋值的情况
while (left <= right) {
int middle = left + ((right - left) / 2);
if (nums[middle] > target) {
right = middle - 1;
} else { // 寻找右边界,nums[middle] == target的时候更新left
left = middle + 1;
rightBorder = left;
}
}
return rightBorder;
}
int getLeftBorder(vector<int>& nums, int target) {
int left = 0;
int right = nums.size() - 1;
int leftBorder = -2; // 记录一下leftBorder没有被赋值的情况
while (left <= right) {
int middle = left + ((right - left) / 2);
if (nums[middle] >= target) { // 寻找左边界,nums[middle] == target的时候更新right
right = middle - 1;
leftBorder = right;
} else {
left = middle + 1;
}
}
return leftBorder;
}
};
69.x的平方根
题目链接:. - 力扣(LeetCode)
class Solution {
public int mySqrt(int x) {
int left = 0;
int right = x;
int ans = -1;
while(left <= right){
int middle = left + ((right - left) / 2);
if((long)middle * middle <= x){
ans = middle;
left = middle + 1;
}else{
right = middle - 1;
}
}
return ans;
}
}
367.有效的完全平方数
题目链接:. - 力扣(LeetCode)
class Solution {
public boolean isPerfectSquare(int num) {
int left = 0;
int right = num;
while(left <= right){
int middle = left + (right - left) / 2;
if((long)middle * middle < num){
left = middle + 1;
}else if((long)middle * middle > num){
right = middle - 1;
}else{
return true;
}
}
return false;
}
}