add: 230401
This commit is contained in:
parent
cec4f35834
commit
aafc1b7c9f
|
|
@ -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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
CMAKE_MINIMUM_REQUIRED(VERSION 3.24)
|
||||||
|
PROJECT(2304)
|
||||||
|
|
||||||
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
|
ADD_EXECUTABLE(2304 230401.cpp)
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue