并查集|栈

lc1668

不能直接跳

class Solution {

public:

int maxRepeating(string sequence, string word) {

int k = 0, n = sequence.size(), wn = word.size(), t = 0;

for (int i = 0; i <= n - wn; i++) {

if (sequence.substr(i, wn) == word) {

t = 1;

++int j = i + wn;++

++while (j++ + wn <= n && sequence.substr(j, wn) == word) {

++t++;++

j += wn;

}

k = max(k, t);

}

}

return k;

}

};

lc969

两次翻转实现煎饼排序:先找到当前未排序部分的最大元素

第一次翻转将其移到数组开头

第二次翻转将其移到当前未排序部分的末尾(即正确位置)

重复此过程直到数组有序,最终返回所有翻转操作的长度记录。

class Solution {

public:

vector<int> pancakeSort(vector<int>& a)

{

int n=a.size();

vector<int> res;

for(int i=n;i>1;--i){

int m=-1,idx=-1;

++for(int j=0;j<i;++j)++

++{
if(aj>m)m=aj,idx=j;
}
++

reverse(a.begin(),a.begin()+idx+1);

reverse(a.begin(),a.begin()+i);

res.push_back(idx+1);

res.push_back(i);

}

return res;

}

};

lc856

用栈处理括号字符串,左括号存0,遇右括号时,若栈顶是0(对应"()")则替换为1

若栈顶是数字(对应"(ABC)")则累加内部数字再乘2

最后把栈里所有分数相加ABC,得到括号的最终分数

class Solution {

public:

int scoreOfParentheses(string S)

{

stack<int> s;

for(char c:S){

if(c=='('){ //遇到左括号入栈,用0模拟

s.push(0);

}

else{ //遇到右括号进行判断

if(s.top()==0){ //栈顶为0即栈顶为左括号,此时为()的情况,得1分

s.pop();

s.push(1);

}

++else{ //栈顶不为左括号即为(ABC)的情况,得分为(A+B+C)*2
int inScore=0;
while(s.top()!=0){++

inScore+=s.top();

s.pop();

}

s.pop();

s.push(inScore*2);

}

}

}

int score=0;

while(!s.empty()){ //最后栈内都是分数,没有括号了,求和即可

score+=s.top();

s.pop();

}

return score;

}

};

lc1319.

dfs

class Solution {

public:

vector<vector<int>> build_graph(const int& n,const vector<vector<int>>& connections) const{

vector<vector<int>> graph(n,vector<int>());

for(auto& edge: connections){

graphedge\[0].push_back(edge1);

graphedge\[1].push_back(edge0);

}

return graph;

}

void dfs(const vector<vector<int>>& graph,unordered_set<int>& visited,int node){

if(visited.count(node))

return;

visited.insert(node);

for(const auto& neighbor: graphnode)

dfs(graph,visited,neighbor);

}

int makeConnected(int n, vector<vector<int>>& connections) {

if(connections.size() < n-1)

return -1;

// 建图

vector<vector<int>> graph = build_graph(n,connections);

unordered_set<int> visited;

int connected_components = 0;

// 遍历顶点

for(int node = 0;node < n;node++){

// 如果没被访问过

if(!visited.count(node)){

connected_components++;

dfs(graph,visited,node);

}

}

return connected_components-1;

}

};

并查集写法

class UnionFind

{

private:

unordered_map<int,int> father;

int num_of_sets;

public:

UnionFind(int size): num_of_sets(size){

for(int i = 0;i < size;i++)

fatheri = i;

}

++int get_num_of_sets(){++

return num_of_sets;

}

++int find(int x){++

if(fatherx == x) return x;

// 路径压缩

fatherx = find(fatherx);

return fatherx;

}

++bool is_connected(int x,int y){++

return find(x) == find(y);

}

++void merge(int x,int y){++

fatherfind(x) = find(y);

num_of_sets--;

}

};

class Solution {

public:

int makeConnected(int n, vector<vector<int>>& connections) {

if(connections.size() < n-1) return -1;

UnionFind uf(n);

for(auto& edge: connections){

if(!uf.is_connected(edge0,edge1)){

uf.merge(edge0,edge1);

}

}

return uf.get_num_of_sets() - 1;

}

};

相关推荐
Fuyo_11191 小时前
C++ AVL树保姆级详解:从平衡因子到四大旋转机制
java·c++·算法
傲世仙尊2 小时前
继承-类设计层次复用切片转换与菱形继承的死结
c++
架构师伯子明2 小时前
FFmpeg C++学习(一):编译和demo
开发语言·c++·学习
纪念 2293 小时前
C++算法(六)
开发语言·c++·算法
ShineWinsu3 小时前
对于Qt:QCheckBox常见属性的详细解析
c++·qt·面试·笔试·gui·qcheckbox·多选框
AI的探索之旅4 小时前
97 个 OpenCV 实例(三十七):二维码识别,一张图四个码的陷阱
c++·opencv·计算机视觉
UIU1144 小时前
P16237 [蓝桥杯 2026 省 B] 应急布线
数据结构·c++·算法·蓝桥杯
raindayinrain4 小时前
c++设计模式
开发语言·c++·设计模式
旖旎夜光4 小时前
LeetCode 315: 算右侧小于当前元素的个数(归并排序) —— 题解
c++·学习·算法·leetcode·排序算法
D e re k ZH4 小时前
保序回归:从 PAVA 到可并堆的单调约束优化
c++·算法·数据挖掘·回归·kotlin