/
DeC2018
/
0033
Обзор
Документация
Войти
/
DeC2018
/
0033
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
main.cpp
52 строки
1 KB
Den
create main.cpp
12 янв 2025, 21:48
12 янв 2025, 21:48
5f3ea11
Код
Авторство
О чём код?
#include <iostream> #include <vector> using namespace std; class Solution { public: int search(vector<int>& nums, int target) { int l = 0; int r = nums.size() - 1; while (l <= r) { const int m = (l + r) / 2; if (nums[m] == target) return m; if (nums[l] <= nums[m]) { // nums[l..m] are sorted if (nums[l] <= target && target < nums[m]) r = m - 1; else l = m + 1; } else { // nums[m..n - 1] are sorted if (nums[m] < target && target <= nums[r]) l = m + 1; else r = m - 1; } } return -1; } }; int main() { Solution solution; vector<int> nums1 = {4, 5, 6, 7, 0, 1, 2}; int target1 = 0; cout << "Input: nums = [4,5,6,7,0,1,2], target = 0" << endl; cout << "Output: " << solution.search(nums1, target1) << endl; vector<int> nums2 = {4, 5, 6, 7, 0, 1, 2}; int target2 = 3; cout << "Input: nums = [4,5,6,7,0,1,2], target = 3" << endl; cout << "Output: " << solution.search(nums2, target2) << endl; vector<int> nums3 = {1}; int target3 = 0; cout << "Input: nums = [1], target = 0" << endl; cout << "Output: " << solution.search(nums3, target3) << endl; return 0; }