![](https://i-blog.csdnimg.cn/direct/c4ccc537ebc746a0af6c05666144c6bb.png)
![](https://i-blog.csdnimg.cn/direct/bc4f4810f0644cf9b7375ff2a8867200.png)
class Solution {
public:
int GetNumberOfK(vector<int>& nums, int k) {
if(nums.size() < 1) return 0;
int left = left_bound(nums, k);
int right = right_bound(nums, k);
return left == -1 ? 0 : right - left + 1;
}
int left_bound(vector<int>& nums, int k){
int left = 0, right = nums.size() - 1;
while(left <= right){
int mid = left + (right - left)/2;
if(nums[mid] >= k) right = mid - 1;
else left = mid + 1;
}
if(left >= nums.size()|| nums[left] != k) return -1;
return left;
}
int right_bound(vector<int>& nums, int k){
int left = 0, right = nums.size() - 1;
while(left <= right){
int mid = left + (right - left)/2;
if(nums[mid] <= k) left = mid + 1;
else right = mid - 1;
}
if(right < 0 || nums[right] != k) return -1;
return right;
}
};
![](https://i-blog.csdnimg.cn/direct/2a704beb3c59422798d520324318b168.png)
class Solution {
public:
int minNumberInRotateArray(vector<int>& nums) {
int left = 0, right = nums.size() - 1;
while(left < right){
int mid = left + (right - left)/2;
if(nums[mid] < nums[right]) right = mid;
else if(nums[mid] == nums[right]) right--; // 缩小范围继续判断
else left = mid + 1;
}
return nums[right];
}
};
![](https://i-blog.csdnimg.cn/direct/de2298b5df5442f08b8c0a77aa941fcc.png)
#include <string>
class Solution {
public:
vector<string> res;
string path;
vector<string> Permutation(string str) {
if(str.empty()) return res;
vector<bool> used(str.size(), false);
backtracking(str, used);
return res;
}
void backtracking(string& str, vector<bool>& used){
if(path.size() == str.size()){
res.push_back(path);
return;
}
unordered_set<char> uset;
for(int i = 0; i < str.size(); i++){
if(used[i] == true || uset.find(str[i]) != uset.end()) continue;
path.push_back(str[i]); used[i] = true;
uset.insert(str[i]);
backtracking(str, used);
path.pop_back(); used[i] = false;
}
}
};
![](https://i-blog.csdnimg.cn/direct/11ecf85eb288473dbb83ec18c4dad556.png)
class Solution {
public:
int minNumberInRotateArray(vector<int>& nums) {
int left = 0, right = nums.size() - 1;
while(left < right){
int mid = left + (right - left)/2;
if(nums[mid] < nums[right]) right = mid;
else if(nums[mid] == nums[right]) right--;
else left = mid + 1;
}
return nums[right];
}
};