首页 > 其他分享 >1. LeetCode 35. 力扣第一题

1. LeetCode 35. 力扣第一题

时间:2023-05-12 22:22:53浏览次数:38  
标签:nums int mid 35 力扣 LeetCode left

按照代码随想录的顺序,今天刷了LeetCode 35.搜索插入位置,也是刷的力扣第一题

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int left = 0;
        int right = nums.size() - 1;
        while(left <= right) {
            int mid = left + (right - left >> 1);
            int x = target - nums[mid];
            if(x == 0) {
                return mid;
            } else if(x < 0) {
                right = mid -1;
            } else if(x > 0) {
                left = mid + 1;
            }
        }
        return left;
    }
};

 

 

标签:nums,int,mid,35,力扣,LeetCode,left
From: https://www.cnblogs.com/konosekai/p/17392542.html

相关文章