#include "s0035_search_insert_position.hpp" // 闭区间写法 int Solution1::searchInsert(vector& nums, int target) { int len = nums.size(); int l{0}; int r = len - 1; while (l <= r) { int m = (l + r) >> 1; if (target < nums[m]) { r = m - 1; } else if (nums[m] < target) { l = m + 1; } else { return m; } } return r + 1; } // 开区间写法 int Solution2::searchInsert(vector& nums, int target) { int len = nums.size(); int l{0}; int r = len; while (l < r) { int m = (l + r) >> 1; if (target < nums[m]) { r = m; } else if (nums[m] < target) { l = m + 1; } else { return m; } } return r; }