并查集|栈

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;

}

};

相关推荐
朝朝辞暮i5 小时前
C++ 第 23 课:class —— 开始真正进入面向对象
开发语言·c++·算法
郑同学的笔记6 小时前
【c++随笔28】strcmp 和 memcmp对比
开发语言·c++
随意起个昵称7 小时前
【背包dp】输出方案路径
c++·动态规划
朝朝辞暮i7 小时前
C++ 第 27 课:智能指针 shared_ptr
开发语言·c++·算法
by209998 小时前
学会使用std::string类,并理解其内部是如何管理字符串的详细阐述(上)
c++·笔记·字符串·类和对象·string
沙漠之主8 小时前
C++编程教学设计资料:从入门到实战的完整课程方案
java·前端·c++
朝朝辞暮i8 小时前
C++ 第 29 课:回调函数 Callback
开发语言·c++·算法
kaixin_learn_qt_ing8 小时前
C++多态实现原理
c++
无名猿8 小时前
unique_ptr 完全指南:独占所有权与零开销
c++·性能优化·内存管理·标准库·现代c++
笑鸿的学习笔记9 小时前
C++笔记之大块顺序写
java·c++·笔记