mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-18 00:57:17 +00:00
Initial commit
This commit is contained in:
24
Easy/search-insert-position/solution.cpp
Normal file
24
Easy/search-insert-position/solution.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user