leetcode 35. 搜索插入位置

题目:35. 搜索插入位置 - 力扣(LeetCode)

加班刷水题

cpp 复制代码
class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        if (target <= nums[0]) return 0;
        if (target > nums[nums.size() - 1]) return nums.size();
        int l = 0;
        int r = nums.size() - 1;
        while (l <= r) {
            int m = (l + r) / 2;
            if (target == nums[m]) return m;
            if (target > nums[m]) {
                if (target < nums[m + 1]) return m + 1;
                l = m + 1;
            } else {
                r = m - 1;
            }
        }
        return -1;
    }
};
相关推荐
Navigator_Z2 小时前
LeetCode //C - 1332. Remove Palindromic Subsequences
c语言·算法·leetcode
Rain的Java大神之路3 小时前
LeetCode 46 全排列:从「及格答案」到让面试官眼前一亮的三种写法
后端·leetcode·面试
Shaoshing5 小时前
LeetCode100 152.乘积最大子数组
数据结构·算法·leetcode
吞下星星的少年·-·5 小时前
力扣 162. 寻找峰值(枚举,二分查找)
算法·leetcode·枚举·二分
Shaoshing21 小时前
LeetCode 921.使括号括号有效的最少添加
java·算法·leetcode·职场和发展
圣保罗的大教堂1 天前
leetcode 3884. 双端字符匹配 简单
leetcode
Shaoshing1 天前
LeetCode100 76.最小覆盖字串
java·算法·leetcode
Navigator_Z1 天前
LeetCode //C - 1331. Rank Transform of an Array
c语言·算法·leetcode
Tisfy1 天前
LeetCode 1202.交换字符串中的元素:分组排序(连通图)
算法·leetcode·字符串·bfs·题解·排序·连通图
小白的高手之路2 天前
【LeetCode 热题 100】206. 反转链表|链表、迭代、递归(Python)
笔记·python·算法·leetcode·链表·职场和发展·力扣