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;
    }
};
相关推荐
Han.miracle1 小时前
数据结构——二叉树的从前序与中序遍历序列构造二叉树
java·数据结构·学习·算法·leetcode
Swift社区7 小时前
LeetCode 401 - 二进制手表
算法·leetcode·ssh
派大星爱吃猫7 小时前
顺序表算法题(LeetCode)
算法·leetcode·职场和发展
Pluchon14 小时前
硅基计划4.0 算法 二叉树深搜(DFS)
java·数据结构·算法·leetcode·深度优先·剪枝
sprintzer15 小时前
10.6-10.15力扣模拟刷题
算法·leetcode·职场和发展
一匹电信狗16 小时前
【C++】C++风格的类型转换
服务器·开发语言·c++·leetcode·小程序·stl·visual studio
学学学无无止境17 小时前
力扣-上升的温度
leetcode
Emilia486.19 小时前
【Leetcode&nowcode&数据结构】顺序表的应用
数据结构·算法·leetcode
Dream it possible!20 小时前
LeetCode 面试经典 150_栈_简化路径(53_71_C++_中等)(栈+stringstream)
c++·leetcode·面试·
程序员阿鹏20 小时前
49.字母异位词分组
java·开发语言·leetcode