代码随想录算法训练营第一天 | LeetCode 704 二分查找 以及LeetCode35,34,69,367

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;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值