mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-17 16:51:46 +00:00
25 lines
484 B
C++
25 lines
484 B
C++
#include <vector>
|
|
|
|
class Solution {
|
|
public:
|
|
int searchInsert(std::vector<int> &nums, int target) {
|
|
int l = 0;
|
|
int h = nums.size() - 1;
|
|
|
|
int m = l + (h - l) / 2;
|
|
while (l <= h) {
|
|
if (nums.at(m) == target) {
|
|
break;
|
|
} else if (nums.at(m) < target) {
|
|
l = m + 1;
|
|
} else {
|
|
h = m - 1;
|
|
}
|
|
|
|
m = l + (h - l) / 2;
|
|
}
|
|
|
|
return m;
|
|
}
|
|
};
|