From 6fdf7e8391a03367df1c457ba5d16424068f0aa1 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Mon, 20 Feb 2023 10:35:58 +0800 Subject: [PATCH] add: 230220 --- cpp/2302/230220.cpp | 16 ++++++++++++++++ cpp/2302/CMakeLists.txt | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 cpp/2302/230220.cpp diff --git a/cpp/2302/230220.cpp b/cpp/2302/230220.cpp new file mode 100644 index 0000000..0dbf5a4 --- /dev/null +++ b/cpp/2302/230220.cpp @@ -0,0 +1,16 @@ +#include +#include + +/** + * 35. Search Insert Position + * + * Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. + * You must write an algorithm with O(log n) runtime complexity. + */ + +class Solution { +public: + static inline int searchInsert(const std::vector& nums, int target) { + return std::lower_bound(nums.begin(), nums.end(), target) - nums.begin(); + } +}; diff --git a/cpp/2302/CMakeLists.txt b/cpp/2302/CMakeLists.txt index e9f1b86..e32d56e 100644 --- a/cpp/2302/CMakeLists.txt +++ b/cpp/2302/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2302) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2302 230219-CN.cpp) +ADD_EXECUTABLE(2302 230220.cpp)