diff --git a/cpp/2304/230401.cpp b/cpp/2304/230401.cpp new file mode 100644 index 0000000..0ad66f5 --- /dev/null +++ b/cpp/2304/230401.cpp @@ -0,0 +1,17 @@ +#include +#include + +/** + * 704. Binary Search + * + * Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1. + * You must write an algorithm with O(log n) runtime complexity. + */ + +class Solution { +public: + static int search(const std::vector& nums, int target) { + auto it = std::lower_bound(nums.begin(), nums.end(), target); + return (it != nums.end() && target == *it) ? it - nums.begin() : -1; + } +}; diff --git a/cpp/2304/CMakeLists.txt b/cpp/2304/CMakeLists.txt new file mode 100644 index 0000000..437204d --- /dev/null +++ b/cpp/2304/CMakeLists.txt @@ -0,0 +1,6 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.24) +PROJECT(2304) + +SET(CMAKE_CXX_STANDARD 23) + +ADD_EXECUTABLE(2304 230401.cpp) diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index b55c4f3..711fcd6 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -28,5 +28,6 @@ ADD_EXECUTABLE(leetcode-cpp main.cpp 2302/230218.cpp 2302/230218-CN.cpp 2302/230 # ADD_SUBDIRECTORY(2207) # ADD_SUBDIRECTORY(2210) # ADD_SUBDIRECTORY(2302) -ADD_SUBDIRECTORY(2303) +# ADD_SUBDIRECTORY(2303) +ADD_SUBDIRECTORY(2304) ADD_SUBDIRECTORY(more)