算法模板
1.二分法
int B_Search(vector<int> nums, int targe){
int left = 0, right = nums.size() - 1;
while(left <= right){
int mid = (left + right)/2;
if(targe > nums[mid]){
left = mid + 1;
}
else if(targe < nums[mid]){
right = mid - 1;
}
else{
return mid;
}
}
return left;
}
2.递归
void dg(int a, int n,int k){
if(arr2.size() == k){
arr.push_back(arr2);
return;
}
if(a > n){
return;
}
arr2.push_back(a);
dg(a+1,n,k);
arr2.pop_back();
dg(a+1,n,k);
}
3.并查集
vector<int> pre;
void ser_zero(int n){
pre.resize(n);
for(int i = 0; i < n; i++){
pre[i] = i;
}
}
int u_search(int root){
int son = root, tmp;
while(root != pre[root]){ //找到根节点
root = pre[root];
}
// while(son != root){ //路径压缩
// tmp = pre[son];
// pre[son] = root;
// son = tmp;
// }
return root;
}