add: 230401

This commit is contained in:
Eatswap 2023-04-01 15:16:00 +08:00
parent cec4f35834
commit aafc1b7c9f
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
3 changed files with 25 additions and 1 deletions

17
cpp/2304/230401.cpp Normal file
View File

@ -0,0 +1,17 @@
#include <vector>
#include <algorithm>
/**
* 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<int>& nums, int target) {
auto it = std::lower_bound(nums.begin(), nums.end(), target);
return (it != nums.end() && target == *it) ? it - nums.begin() : -1;
}
};

6
cpp/2304/CMakeLists.txt Normal file
View File

@ -0,0 +1,6 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.24)
PROJECT(2304)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2304 230401.cpp)

View File

@ -28,5 +28,6 @@ ADD_EXECUTABLE(leetcode-cpp main.cpp 2302/230218.cpp 2302/230218-CN.cpp 2302/230
# ADD_SUBDIRECTORY(2207) # ADD_SUBDIRECTORY(2207)
# ADD_SUBDIRECTORY(2210) # ADD_SUBDIRECTORY(2210)
# ADD_SUBDIRECTORY(2302) # ADD_SUBDIRECTORY(2302)
ADD_SUBDIRECTORY(2303) # ADD_SUBDIRECTORY(2303)
ADD_SUBDIRECTORY(2304)
ADD_SUBDIRECTORY(more) ADD_SUBDIRECTORY(more)