Created
February 1, 2025 11:08
-
-
Save OriLiMu/32af68b302ae9008f15387d47c3184b5 to your computer and use it in GitHub Desktop.
找到转折点
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <climits> | |
#include <cmath> | |
#include <cstddef> | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
class Solution { | |
public: | |
bool search(vector<int> &nums, int target) { | |
int leftIndex = 0; | |
int rightIndex = nums.size() - 1; | |
int midIndex = (leftIndex + rightIndex) / 2; | |
while (true) { | |
if (midIndex > 0 && midIndex < nums.size() - 1 && | |
nums[midIndex - 1] > nums[midIndex] && | |
nums[midIndex + 1] > nums[midIndex]) { | |
break; | |
} else if (midIndex == 0 || midIndex == nums.size() - 1) { | |
break; | |
} | |
if (nums[leftIndex] > nums[midIndex]) { | |
rightIndex = midIndex; | |
midIndex = (leftIndex + midIndex) / 2; | |
} else { | |
leftIndex = midIndex; | |
midIndex = (rightIndex + midIndex) / 2; | |
} | |
} | |
return false; | |
} | |
}; | |
int main() { | |
Solution s; | |
vector<int> vec = { | |
1, | |
}; | |
cout << s.search(vec, 0) << endl; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment